简体   繁体   中英

SQL and PHP: Find all “assets” connected to “presentation”

I really need some help with some SQL.

Question 1:

It's easy enough to get all "assets" that belong to a "presentation" if the asset has a corresponding "presentationid" within it's table.

SELECT * FROM asset WHERE presentationid = 3

But how to I accomplish the same thing by joining? What's the best way to say:

SELECT * FROM asset WHERE ... asset is connected to presentation via "presentationasset":

TABLE asset
id
name

TABLE presentation
id
name

TABLE presenationasset
id
presentationid
assetid

I hope this makes sense. I want to list out all of the actual assets and their columns, not the association table. :)

Question 2: (not as important)

I have my application setup so that "presentation" is a class and "asset" is a class...

With question 1 in mind, how do I return each of the associated assets as "asset" objects? Or does that even matter?

Q1:

SELECT a.id, a.name
FROM asset a 
JOIN presentationasset pa ON pa.assetid = a.id AND pa.presentationid = 3
JOIN presentation p ON p.id = pa.presentationid

Fair enough :)

Select * from asset where id in
(select assetid from presentationasset where id in
(select id from presentation where name = "whateva"))

or

Select * from asset where id in
(select assetid from presentationasset where presentationid = 3)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM