MySql从多个表中选择数据

时间:2013-07-08 18:31:20

标签: mysql select

这是我目前的查询:

SELECT * FROM images T 
JOIN boxes_items T2 
ON T.ITEM_ID = T2.ITEM_PARENT_ID 
WHERE T2.ITEM_ID = '$image_id' 

我还需要从名为'boxes'的表中选择all,其中box_id取自boxes_items。
如何将其添加到查询中?

3 个答案:

答案 0 :(得分:2)

SELECT * 
FROM images T 
JOIN boxes_items T2 ON T.ITEM_ID = T2.ITEM_PARENT_ID 
JOIN boxes AS b ON b.box_id = t2.box_id
WHERE T2.ITEM_ID = '$image_id' 

答案 1 :(得分:2)

试试这个

  SELECT T.* , T2.* , T3.* FROM images T 
  JOIN boxes_items T2  ON T.ITEM_ID = T2.ITEM_PARENT_ID 
  JOIN boxes  T3 ON T3.box_id = t2.box_id
  WHERE T2.ITEM_ID = '$image_id' 

答案 2 :(得分:1)

你真的不应该使用*,它会让你陷入比它更值得的麻烦。特别是因为您的查询中有多个表。

反正:

Select T.*, T2.*, T3.*
from images T
join boxes_items T2 on T.ITEM_ID = T2.ITEM_PARENT_ID
join boxes T3 on T3.box_id = T2.box_id
WHERE T2.ITEM_ID = '$image_id'