DasBoot
DasBoot

Reputation: 707

sql query join query output gives duplicates

So i wanted to extract information from 3 tables at the same time, but whenever i update my query on table1 it gives me duplicates and overrides the previous entries, like in row n in garage column if i update garage to red, it will display red for the previous entry as well. any thoughts on how to do this?

SELECT `date`,`tagid`,`garage`,`class` 
FROM table1 JOIN table2 ON table1.number = table2.tagid
UNION SELECT `date`,`tagid`,`garage`,`class` 
FROM table1 JOIN table3 ON table1.number = table3.tagid

Upvotes: 0

Views: 178

Answers (1)

Puggan Se
Puggan Se

Reputation: 5846

It looks like you want to do somthing like:

SELECT date, tagid, garage, class 
FROM table1 
LEFT JOIN (
    SELECT date, tagid, garage, class FROM table2 
    UNION
    SELECT date, tagid, garage, class FROM table3 
) AS table_2_and_3 ON (table1.number = table_2_and_3.tagid)

you may need to remove some fields on row 4 and 6 if they are in table 1.

Upvotes: 1

Related Questions