Randum Ian wrote:
Hi guys,
Wonder if you can help me with this.
I have a "recording_global" table which has "recording_global_id" in and
"dj_global_id".
I want to be able to sort the "recording_global" table alphabetically by
dj name which I need to find from the "dj_global_id" table referencing
it from the ID in the "recording_global" table and enter it into a
script.
I have been banging my brain all afternoon but am no nearer to the
solution. Below is the code I got upto before I realised I was stuck.
$sql = "SELECT * FROM recording_global ORDER BY recording_global_name
WHERE recording_global_name LIKE a%";
Any help hugely appreciated! Ian.



Okay, I'm


So we have a recording_global table, with dj_global_id and a recording_global_id

We have a dj_global table that associates dj names with dj_global_id's.

( I'm not certain what your exact table names are, making the table name the same as a column inside it is confusing )

So, the following should work, at least in oracle... <:) You may need to tweak for whatever DB you use.... ( If you are using Oracle, Oracle has GREAT SQL documentation, I've found it incredibly helpful )

The line numbers are there for the explanation, do not enter them... <:)

1) SELECT *.recording, last_name.dj, first_name.dj
2)      FROM recording_global recording, dj_global dj
3)      WHERE dj_global_id.dj = dj_global_id.recording
4)      ORDER BY last_name.dj ASC, first_name.dj ASC

1) selects all columns from the recording table, and the first and last names of the dj from the dj table. These are the columns that will be returned from the DB.

2) Declares the tables to select from, and assigns aliases to the table names, so that line 1 (And other lines ) are easier to type..
Thus dj_global can be refered to as dj in the rest of the query...


3) We're intrested only in entries that have matching dj_global_ids in both the recording and dj tables.... Note that if a recording has no dj_global_id assigned to it, it will not be returned in the dataset. ( Thus we can start talking about inner/outer/left/right joins )...

4) We now have a dataset containing all the data columns from recording with the dj names from the dj table. We ask it to sort the data set by dj last_name ( in ascending order ) then subsort by dj first_name ( in ascending order ).

I think this should work, if I understand your db table layout properly...

-Daniel


-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to