2 回答

TA貢獻(xiàn)1155條經(jīng)驗(yàn) 獲得超0個(gè)贊
首先,當(dāng)你使用SELECT *mysql時(shí),會(huì)加載from和join中所有表的所有字段。MySQL表中的同名字段會(huì)互相覆蓋,那么mysql將只返回最后一個(gè)表的字段(即您查詢中的Table6)。如果您執(zhí)行該查詢:
SELECT * FROM `Table1` WHERE `product_code` = 100001
UNION SELECT * FROM `Table2` WHERE `product_code` = 100001
UNION SELECT * FROM `Table3` WHERE `product_code` = 100001
UNION SELECT * FROM `Table4` WHERE `product_code` = 100001
UNION SELECT * FROM `Table5` WHERE `product_code` = 100001
UNION SELECT * FROM `Table5` WHERE `product_code` = 100001
Mysql 將返回(不完全按照該順序):
product_code - product_price
100001 - 100
100001 - 200
100001 - 300
100001 - 400
100001 - 500
100001 - 600
如果需要,您可以添加ORDER BY product_price ASC LIMIT 1;或。DESC
但僅當(dāng)您正在學(xué)習(xí)時(shí)才這樣做,在實(shí)際應(yīng)用程序中,不建議在不同表的字段中使用相同的名稱。

TA貢獻(xiàn)1725條經(jīng)驗(yàn) 獲得超8個(gè)贊
我認(rèn)為你想使用 UNION 語(yǔ)句來(lái)獲取所有價(jià)格,然后你可以排序并將第一個(gè)結(jié)果作為最低價(jià)格:
SELECT `product_price` FROM `Table1` WHERE `product_code` = 100001
UNION SELECT `product_price` FROM `Table2` WHERE `product_code` = 100001
UNION SELECT `product_price` FROM `Table3` WHERE `product_code` = 100001
UNION SELECT `product_price` FROM `Table4` WHERE `product_code` = 100001
UNION SELECT `product_price` FROM `Table5` WHERE `product_code` = 100001
UNION SELECT `product_price` FROM `Table5` WHERE `product_code` = 100001
ORDER BY 1 ASC LIMIT 1;
- 2 回答
- 0 關(guān)注
- 174 瀏覽
添加回答
舉報(bào)