Jump to content

select statement from multiple tables


littledragon

Recommended Posts

Hi guys, have been googling this for an hour now, not getting anywhere, maybe am getting tired... :)

 

I have three tables (files, media and content), mostly different except all have the columns id, par, basename, & path

 

I need a single statement where I can select id, basename, path FROM  all three tables  WHERE par = 'a_value',  ORDER BY basename

 

??? any ideas?  _Much_ appreciated!

 

ld

Link to comment
https://forums.phpfreaks.com/topic/106610-select-statement-from-multiple-tables/
Share on other sites

SELECT files.id, files.basename, files.path,  media.id, media.basename, media.path, content.id,

content.basename, content.path

FROM files INNER JOIN media  ON files.par =  media.par

INNER JOIN content  ON WHERE media.par =  content.par

WHERE content.par = a_value',  ORDER BY basename

SORRY

 

SELECT files.id, files.basename, files.path,  media.id, media.basename, media.path, content.id,

content.basename, content.path

FROM files INNER JOIN media  ON files.par =  media.par

INNER JOIN content  ON  media.par =  content.par

WHERE content.par = a_value',  ORDER BY basename

Yeah, but I want it to be ordered by basename across all tables, so some kind of join is needed at basename and then order by it - I just can't find how to do this

 

In any case, if I remove the order clause in the above example I get a NULL result, even though a simple select on any one of the tables yields results for par = a_value

 

Let me know if my problem is still ambiguous

is it imperative that you have one single statement? can you not do this in nested statements? you would have more control over what its going on. i know it would be better coding practice to write a join however if youre not getting out of it what you need then maybe you should consider a different way of doing it.

I agree, overheads are not really an issue on this site, but it seems silly to go pulling apart and reassembling the arrays to get them all organised by the one field (cos if I do three seperate queries then I get three *seperate* nicely organised arrays rather than one) when I know mysql is capable of doing it for me

 

And also, I'd like to know how to do it :)

ok thats a fair enough point and a good enough reason to persist with one statement.

 

is it not possible to say ORDER BY files.basename, media.basename, content.basename so that it would first order by the files.basename and then within that order by media.basename etc etc.

 

just a thought

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.