bushdiver
bushdiver

Reputation: 771

MySql Select data from multiple tables

This is my current query:

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

I also need to Select all from a table named 'boxes' where the box_id is taken from boxes_items.
How to add this to the query?

Upvotes: 0

Views: 89

Answers (3)

echo_Me
echo_Me

Reputation: 37233

try this

  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' 

Upvotes: 3

waka
waka

Reputation: 3407

You really shouldn't use *, it can get you into more trouble than it is worth. Especially since you have more than one table in your query.

Anyway:

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'

Upvotes: 1

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79919

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' 

Upvotes: 2

Related Questions