[英]How to get all columns from one table and only one column from another table with ID ? - MySql
[英]How to get all columns from one table and one column from another table in SQLITE
我有 2 个具有相同列名但具有不同数据的表。 考虑以下:
表1结构:
id, sid, text
1, 1, 'Hello World'
2, 1, 'Hello World again'
3, 2, 'Hw...'
4, 2, 'Hw2...'
和表2:
id, sid, text
1, 1, 'Hello World 2'
2, 1, 'Hello World again 2'
3, 2, 'Hw2...'
4, 2, 'Hw22...'
我想根据“sid”查询“Table1”。 例如,如果 sid 为 1,那么我应该得到 2 行结果,其中包含“Table1”中的所有列,但只有“Table2”中的“text”列。 像这样:
id, sid, text, text
1, 1, 'Hello World', 'Hello World2'
2, 1, 'Hello World again', 'Hello World again 2'
我正在尝试 INNER JOIN 但我得到了重复:
SELECT Table1.*, Table2.text FROM Table1 INNER JOIN Table2 ON Table1.sid = Table2.sid WHERE Table1.sid = 1;
但我得到重复的行? 我该如何解决这个问题。 我知道有很多类似的问题,但我找不到有这个问题的问题。
您必须使用 sid 和 id 来识别正确的行
SELECT
t1.*, t2.text
FROM
Table1 t1
INNER JOIN
Table2 t2 ON t1.sid = t2.sid AND t1.id = t2.id
WHERE
Tt1.sid = 1;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.