我想通過不同的表通過單個sql語句檢索一些信息。 我想做類似:從其他表中選擇列作爲select語句?
select col1,
col2,
(select col1
from table 2),
(select col1
from table 3),
from table1
join table2
join table3
任何人都可以幫忙嗎?
我想通過不同的表通過單個sql語句檢索一些信息。 我想做類似:從其他表中選擇列作爲select語句?
select col1,
col2,
(select col1
from table 2),
(select col1
from table 3),
from table1
join table2
join table3
任何人都可以幫忙嗎?
首先,決定如何獲取數據。如果你想使用子查詢,那麼罰款,否則使用連接。例如,子查詢可能是這樣的:
select t1.col1,
t1.col2,
(select col1 from table2 t2 where t2.field = t1.field),
(select col1 from table3 t3 where t3.field = t1.field)
from table1 t1
相反,如果你想使用連接,它可能是這樣的:
select t1.col1,
t1.col2,
t2.col1,
t3.col1
from table1 t1
join table2 t2 on t2.field = t1.field
join table3 t3 on t3.field = t1.field
select table1.col1 as t1c1, table1.col2 as t1c2, table2.col1 as t2c1, table3.col1 as t3c1
from table1
join table2
join table3
注意您需要實際參加表2和表3 ......加入這樣的語句將不會工作,他們沒有ON部分。
我明白了,我只是寫它作爲例子,謝謝:) –
請添加表結構,例如數據和預期產出。 –
我上面寫了三張表,而table1與table2和table3有關。表1包含兩列,例如:col3和col4,分別存儲table2和table3的col1值。其實桌子很大,我不認爲在這裏複製它們並不好。 –
只是一個獎金珍聞。如果這些表在不同的數據庫中,你也可以使用'Database_name'.'Table_Name'來引用相應的表 – Mason