Jump to content

[SOLVED] Order By Clause not working


tpra21

Recommended Posts

I cannot get the ORDER BY clause in the following query to work:

 

SELECT count(*) FROM ads, school WHERE ads.avg_rent >=0 AND ads.avg_rent <=99999999 AND ads.university=school.school_ID AND ads.university =1728 ORDER BY ads.expirey, ads.expirem, ads.expired DESC

 

With the above query, I tried including the ORDER BY fields in the selection with the count(*), but then my query returned no results.

 

The query itself returns the correct rows, but it doesn't order them. Also, is it possible to rearrange a field in the table to place in the ORDER BY clause? For example, I want to order the results by an ad's posted date, but it is in mm-dd-yyyy format and is a text field. Is there any way for me to re-format the text field into ISO format and then use that format in the order by clause?

 

Thanks,

 

Link to comment
Share on other sites

That is what it is doing. I didn't write the code, but from what I have seen it counts all the records, and then uses a list() function to read through the result records. The code follows:

 


       $maxQR = mysql_db_query($db_name, $query, $connection);

        if(!$maxQR){
        // die - error here
        }

        list($d_max)= mysql_fetch_row($maxQR);
        if($d_max > 0){
          // if rows, list them here
        }

Link to comment
Share on other sites

Lets leave PHP out of this for a moment and just look at your results by running the query in phpMyAdmin, MySQL Query Browser or similar.

 

You'll find there is nothing to order by or iterate thru with list.

SELECT COUNT(*) AS cnt 
FROM ads
JOIN school AS s ON ads.university=s.school_ID

 

Will return something like this:

cnt

-----

324

 

Not very interesting. Try something along these lines:

SELECT s.*, count(ads.ID) AS cnt
FROM ads
JOIN school AS s ON ads.university=school.school_ID 
WHERE ads.avg_rent BETWEEN 0 AND 99999999 
GROUP BY s.school_ID ORDER BY cnt DESC

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.