Jump to content

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


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

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.