0

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?

2
  • Your description isn't clear enough, can't tell whether you need a UNION or another JOIN. Can you add sample data and the desired result? Commented Jul 8, 2013 at 18:33
  • @Barmar I think I need another Join - I need to Select all from Another table named "Boxes" where the box_id = box_id collected from "boxes_items" WHERE T2.ITEM_ID = '$image_id' Commented Jul 8, 2013 at 18:35

3 Answers 3

3

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' 
Sign up to request clarification or add additional context in comments.

4 Comments

What's the difference between this and the original query? Where is the table boxes?
@Barmar you have good eyes barmar :) , can you sell me one :) ?
How to differentiate between rows after results? $row['T.something'] ?
call them like that : $row['something'] , and columns if they have same name give them a different aliases AS ... and then call them as normal
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' 

Comments

1

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'

2 Comments

I think this will do it, but how do I differentiate between the rows when assigning variables? using: $result = mysql_query($query); while ($row = mysql_fetch_array($result)) { }
@bushdiver: You can differentiate between rows using the index of the rows. Usually this index is zero based, meaning that the first row has the index 0, the second index 1, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.