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
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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.