I have 2 tables with the same column names but with different data. Consider the following:
Table1 Structure:
id, sid, text
1, 1, 'Hello World'
2, 1, 'Hello World again'
3, 2, 'Hw...'
4, 2, 'Hw2...'
And Table2:
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". 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". 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:
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
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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.