- Nov 20, 2005
- 6,161
- 2
- 81
I am having problems figuring out this SQL query. It keeps printing duplicate data into the table, but I only want unique data. Basically, I have 4 tables, maintable, table1, table2, table3. Tables 1-3 have a foreign key linked to maintable.
I am getting data like this:
But I want it to be displayed like this:
Query:
I've tried various different types of joins. I even tried a subquery for the where clause. Can't seem to figure it out.
I am getting data like this:
Code:
Table1 Table2 Table3
Something here table 2 stuff table 3 stuff
Different table 2 different stuff Ditto
Different still Same as above Ditto
Code:
Table1 Table2 Table3
Something here table 2 stuff table 3 stuff
Different table 2 different stuff
still
Query:
Code:
SELECT DISTINCT o.name, d.name, a.name
FROM maintable AS f
LEFT JOIN table1 AS o ON f.tableid=o.tab_id
LEFT JOIN table2 AS d ON f.tableid=d.tab_id
LEFT JOIN table3 AS a ON f.tableid=a.tab_id
WHERE f.name = "Something"
I've tried various different types of joins. I even tried a subquery for the where clause. Can't seem to figure it out.
Last edited: