473,407 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

MySQL, ORDER BY Multiple, 2nd ARG is ignored.

angelopc
I'm trying to retrieve some results from a table. I want the results chosen randomly, then sorted by the number of "hits".

Here's the code I'm using:
Expand|Select|Wrap|Line Numbers
  1. <?php $latestQuery = "SELECT * FROM pages ORDER BY RAND(), hits DESC LIMIT 100"; ?>
With this syntax, the results are chosen randomly, but they are not sorted by "hits".

You can see the results at Insoogle.com (scroll to latest searches).

I'm sure there's a simple fix for the code I'm using. I just can't seem to figure it out.
Dec 13 '09 #1
2 2276
Atli
5,058 Expert 4TB
Hey.

The problem is that the ORDER BY RAND(), which you use to fetch 100 random rows, is also the primary sort method for those 100 records. The secondary sort column (hits) is never used unless there happen to be two identical rows next to each other in the random list.

What you need to do is first retrieve the 100 records, and then sort them.
Something like:
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM (
  2.     SELECT * 
  3.     FROM pages 
  4.     ORDER BY RAND()
  5.     LIMIT 100
  6. ) AS `random_pages`
  7. ORDER BY hits DESC 
I'm betting there is a more optimized way to do this, but I'm to tired to see it at the moment xD
Dec 13 '09 #2
Bingo! Thank you, Atli. I never would have known to do that. Worked like a charm!
Dec 13 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Rahul Anand | last post by:
Hi all, I am trying to fetch 5 records, randomly picked and in random order from a MySQL table (MySQL version > 3.23). I wrote my SQL Query as SELECT name,id FROM tablename WHERE active =...
7
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
0
by: Lenz Grimmer | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, MySQL 4.0.14, a new version of the popular Open Source/Free Software Database, has been released. It is now available in source and binary...
0
by: Robert Mazur | last post by:
Solaris 9 - sparc 64bit MySQL 5.0 alpha (installed using binary from MySQL) --------------------------------- Has anyone expereinced this? The client will launch locally and process SQL...
1
by: smsabu2002 | last post by:
Hi, I am facing the build problem while installing the DBD-MySql perl module (ver 2.9008) using both GCC and CC compilers in HP-UX machine. For the Build using GCC, the compiler error is...
0
by: pvannie | last post by:
Hello there, I've been trying to install DBD::mysql on Mac OS X Server 10.3 for 2 days, but it still raises error. So I'm going to send this message to ask for help. Any help will be much...
1
by: pvannie | last post by:
Hello there, I've been trying to install DBD::mysql on Mac OS X Server 10.3 for 2 days, but it still raises error. So I'm going to send this message to ask for help. Any help will be much...
6
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get...
1
by: Cappamontana | last post by:
Hi there, Now I've read the ORDER BY section of the mysql documentation, but still have no clue, what strategy I have to follow now. The problem is that I want to query the DB like: SELECT *...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.