Jump to content

[SOLVED] Adding "ORDER BY" messes up my query. Help, please!...


ShootingBlanks

Recommended Posts

Hello.  I have the following query that works and brings up the first 15 records in my database table:

SELECT documents.doc_id, documents.title_full, documents.updated,
DATE_FORMAT(documents.updated, '%m-%d-%Y') AS updated
FROM documents, doc_cat_lookup
WHERE documents.live_doc = 'y'
LIMIT 15

 

Now, I want to only show the ones that were updated the most recently, so I added an "ORDER BY" line after the "WHERE" line to make the query this:

SELECT documents.doc_id, documents.title_full, documents.updated,
DATE_FORMAT(documents.updated, '%m-%d-%Y') AS updated
FROM documents, doc_cat_lookup
WHERE documents.live_doc = 'y'
ORDER BY updated DESC, title_full ASC
LIMIT 15

 

However, now all that comes up is the one most recently updated record, repeated 15 times.  Any ideas where I'm going wrong here???

 

Thanks!!!

 

 

I think this happens because you didn't specify a relation between the two tables (WHERE documents.id = doc_cat_lookup.id, for example).  If you have N records in documents, and M records in doc_cat_lookup, and you are getting MxN results.

 

The reason you didn't see it before is because you had the LIMIT statement (try taking it out from the original query and you'll see what I mean).

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.