php - How to make a join statement between multi table to one table? -
first, have 4 table , columns such
- feeds (id, type, type_id)
- feeds_normals (id, type, content)
- feeds_links (id, type, title, link)
- feeds_youtubes (id, type, title, link, description, image)
- feeds_photos (id, type, link)
the table of "feeds type_id" match/linkup "id" of normals, links, youtubes, photos
and
the table of "feeds type" using identified table should joined
for example:
feeds: id: 1, type: "normal", type_id: 1 id: 2, type: "link", type_id: 1 feeds_normals: id: 1, type: "normal", content: "this test" feeds_links: id: 1, type: "link", title: "this title", link: "http://yahoo.com"
result:
id: 1, type: "normal", content: "this test", title: null, link: null id: 2, type: "link", content: null, title: "this title", link: "http://yahoo.com"
finally
in case, how write sql statement?
something wrong here, may example or actual data. here how super-type/subtype model looks like:
the matching data-example be:
feeds: feedid: 1, type: "normal" feedid: 2, type: "link" feeds_normals: feedid: 1, content: "this test" feeds_links: feedid: 2, title: "this title", link: "http://yahoo.com"
note feedid
in subtype table matches 1 in super-type. type
field in subtype tables optional -- allows check constraint enforce types not mixed in subtype table.
the query like:
select f.feedid , n.content normalscontent , y.title youtubetitle , y.link youtubelink , y.description youtubedescription , y.image youtueimage , k.title linkstitle , k.link linkslink feeds f left join feeds_normals n on n.feedid = f.feedid left join feeds_links k on k.feedid = f.feedid left join feeds_youtube y on y.feedid = f.feedid ;
Comments
Post a Comment