0

I need some help with a specific problem about subqueries in MySQL. This is a shortversion of the problem I have:

I have table 1 and 2 where table 1 have a column called "SupplyID" which corresponds with table 2's "ID". I need to get a list with stuff from one single supplier.

I tried with this but didn't work:

select name
from item
where SupplyID exists (
   select *
   from SupplyID
   where SupplyID = ID
);
1
  • You need this if you want to show it in one table Commented Jan 21, 2017 at 12:51

3 Answers 3

2

Assuming your tables are named table1 and table2 you You could use a inner join

select distinct t1.name
from ybale  as t1 
inner join table2 as t2 on t1.ID = t2.SupplyID
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

select name from item i where exists (select * from table2 t where i.SupplyID = t.ID);

1 Comment

Thanks! This worked but what if I need to get one single ID? Is there an easier solution than needing to have the ID in the command?
0

My answer is more like to what scaisEdge answered here but I strongly recommend to do this with LEFT JOIN. and If you want to select just one ID, for example ID=10

SELECT T1.name
FROM item AS T1
LEFT JOIN SupplyID AS T2 ON T2.SupplyID=T1.ID
WHERE T1.ID = 10

Comments

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.