是否有任何方法可以進行 MySQL 多表選擇,可以標記/標記列源自的表中的列?
table1
tID name desc
---------------------
1 foo lots of foo
2 bar lots of bar
3 foobar none of these
table2
oID tID orderNo
------------------------
1 1 19001
2 1 19002
3 3 19004
4 2 19005
SELECT t1.*, t2.* FROM table1 t1,table2 t2 WHERE t1.tID=t2.tID ;
輸出將是:
tID name desc oID orderNo
-----------------------------------------------
1 foo lots of foo 1 19001
1 foo lots of foo 1 19002
2 foo lots of bar 4 19005
3 foo none of these 3 19004
但我正在尋找輸出如下的東西:
t1.tID t1.name t1.desc t2.oID t2.orderNo
---------------------------------------------------------------
1 foo lots of foo 1 19001
1 foo lots of foo 1 19002
2 foo lots of bar 4 19005
3 foo none of these 3 19004
這個小例子代表了我試圖解決的一個更大的問題。它將允許進行一次查詢,但也能夠在決議資料時告訴我資料來自哪個表。
uj5u.com熱心網友回復:
您可以為每一列使用別名,我建議撰寫適當的連接。
SELECT t1.tID as `t1.tID`,
t1.name as `t1.name`,
t1.desc as `t1.desc`,
t2.oID as `t2.oID`,
t2.orderNo as `t2.orderNo`
FROM table1 t1
INNER JOIN table2 on t1.tID=t2.tID ;
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/315205.html
