我有 2 个表,需要连接它们才能获得所需的结果。我尝试了多种类型的连接,但没有成功。 请帮忙。下面是我的表格的样子:
从 t1 中选择 *
ProductId, Sequence, Property
100, 1, Size
100, 2, Folder
100, 3, License
101, 1, Usage
101, 2, Duration
从 t2 中选择 *
SrNo, ProductId, Property, PropertyValue
1, 100, Size, 10GB
2, 100, Folder, /home/path
3, 101, Usage, Database
我需要加入他们才能得到以下结果:
SrNo, ProductId, Sequence, Property, PropertyValue
1 100 1 Size 10GB
1 100 2 Folder
1 100 3 License
2 100 1 Size
2 100 2 Folder /home/path
2 100 3 License
3 101 1 Usage Database
3 101 2 Duration
下面是重现相同表的 SQL:
create table t1 (ProductId INT, Sequence INT, Property VARCHAR(255))
insert into t1 values(100,1,'Size');
insert into t1 values(100,2,'Folder');
insert into t1 values(100,3,'License');
insert into t1 values(101,1,'Usage');
insert into t1 values(101,2,'Duration');
create table t2 (SrNo INT, ProductId INT, Property VARCHAR(255), PropertyValue VARCHAR(255))
insert into t2 values(1,100,'Size','10GB');
insert into t2 values(2,100,'Folder','/home/path');
insert into t2 values(3,101,'Usage','Database');
请帮助我如何编写查询?
最佳答案
SELECT
table2.SrNo,
table1.ProductId,
table1.Sequence,
table1.Property,
table2.PropertyValue
FROM t1 AS table1
JOIN t2 AS table2 ON table1.ProductId = table2.ProductId
关于mysql - SQL Join 查询连接 2 个表以获得所需的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35273159/