[英]How to get all columns from one table and one column from another table in SQLITE
I have 2 tables with the same column names but with different data.我有 2 个具有相同列名但具有不同数据的表。 Consider the following:考虑以下:
Table1 Structure:表1结构:
id, sid, text
1, 1, 'Hello World'
2, 1, 'Hello World again'
3, 2, 'Hw...'
4, 2, 'Hw2...'
And Table2:和表2:
id, sid, text
1, 1, 'Hello World 2'
2, 1, 'Hello World again 2'
3, 2, 'Hw2...'
4, 2, 'Hw22...'
I want to query "Table1" based on "sid".我想根据“sid”查询“Table1”。 For example, if sid is 1 then I should get 2 rows as a result with all columns from "Table1" but just column "text" from "Table2".例如,如果 sid 为 1,那么我应该得到 2 行结果,其中包含“Table1”中的所有列,但只有“Table2”中的“text”列。 Like this:像这样:
id, sid, text, text
1, 1, 'Hello World', 'Hello World2'
2, 1, 'Hello World again', 'Hello World again 2'
I am trying to INNER JOIN But I get repition:我正在尝试 INNER JOIN 但我得到了重复:
SELECT Table1.*, Table2.text FROM Table1 INNER JOIN Table2 ON Table1.sid = Table2.sid WHERE Table1.sid = 1;
But I am getting duplicate rows?但我得到重复的行? How can I solve this.我该如何解决这个问题。 I know there are many similar questions but I could not find one with this problem.我知道有很多类似的问题,但我找不到有这个问题的问题。
You have to use sid and id to identify the right row您必须使用 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.