开发者

Selecting data from multiple tables and use a left join?

开发者 https://www.devze.com 2023-01-23 20:23 出处:网络
I have a query which doesnt work and it\'s strating to drive me crazy ;). Basically this is what I\'m trying to achieve:

I have a query which doesnt work and it's strating to drive me crazy ;). Basically this is what I'm trying to achieve:

SELECT
  table1.field1,
  table1.field2,
  table2.field1,
  table3.field1
FROM
  table1,
  table2
LEFT JOIN
  table3
ON table3.field1 = table2.field1

But for some reason I'm unable to execute this query. I believe th开发者_运维问答is is because of the join being executed after telling to select from multiple tables. Because this query works when I select from 1 table.

Can someone tell me what to do when I want to select from multiple tables and use joins?


You are missing a JOIN condition between table1 and table2:

SELECT  table1.field1,
        table1.field2,
        table2.field1,
        table3.field1
FROM    table1
LEFT JOIN
        table2
ON      table2.id = table1.id
LEFT JOIN
        table3
ON      table3.field1 = table2.field1


You probably want a JOIN and ON clause for your first join. Something like:

SELECT 
  t1.field1, 
  t1.field2, 
  t2.field1, 
  t3.field1 
FROM table1 t1
INNER JOIN table2 t2 on t1.ID = t2.ID
LEFT JOIN table3 t3 ON t3.field1 = t2.field1 

Note, the INNER JOIN may need to be a LEFT JOIN, it is not clear from your question.

0

精彩评论

暂无评论...
验证码 换一张
取 消