Solved

Select Random records then sort them.

Posted on 2007-04-02
6
546 Views
Last Modified: 2011-10-03
I want to pull the first 10 random record, but then I want them in order by customer name.

The following query pulls the random 10 records, but I can't get them in order without messing up the random selection.

SELECT tblProducts.ProductID, tblProducts.Description, tblProducts.CustomerSKU,
tblProducts.QtyPerUnit, tblProducts.Units, tblCustomers.Name,
tblInventory.QtyOnHand, tblProducts.PickLocation
FROM tblProducts
INNER JOIN tblInventory ON tblInventory.ProductID = tblProducts.ProductID
INNER JOIN tblCustomers ON tblCustomers.CustomerID = tblProducts.CustomerID
WHERE tblInventory.QtyOnHand > 0
AND tblCustomers.liveCustomer = 1
AND (tblInventory.AuditDate >= '04/02/2006' OR tblInventory.AuditDate IS NULL)
ORDER BY RAND() LIMIT 10
0
Comment
Question by:CtrlAltDl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18839554
SELECT * FROM `table` ORDER BY RAND() LIMIT 10;
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 18839560
sorry... just a question: are you actually using MySQL or MS SQL Server?
also, which version?

I fear, that with MySQL you need to insert your select query into a temp table, and then query from that table with the final order by
in MS SSQL, it would be easier...


0
 
LVL 14

Author Comment

by:CtrlAltDl
ID: 18839577
It's MySQL 4.x and I'm programming in PHP 4.3.x.

I was thinking that I would have to put it in a temp table first, but I'm not sure my MySQL supports that or not.  It seems like I should be able to do a subquery with RAND() then I can sort them in the main query.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18839591
MySQL 4.x -> sorry, no subqueries as needed for that.
also, LIMIT in subqueries is also not available.
0
 
LVL 14

Author Comment

by:CtrlAltDl
ID: 18839691
I was able to do it with a Temp table:

CREATE TEMPORARY TABLE tempTable
SELECT tblProducts.ProductID, tblProducts.Description, tblProducts.CustomerSKU,
tblProducts.QtyPerUnit, tblProducts.Units, tblCustomers.Name,
tblInventory.QtyOnHand, tblProducts.PickLocation
FROM tblProducts
INNER JOIN tblInventory ON tblInventory.ProductID = tblProducts.ProductID
INNER JOIN tblCustomers ON tblCustomers.CustomerID = tblProducts.CustomerID
WHERE tblInventory.QtyOnHand > 0
AND tblCustomers.liveCustomer = 1
AND (tblInventory.AuditDate >= '04/02/2006' OR tblInventory.AuditDate IS NULL)
ORDER BY RAND()
LIMIT 18;
SELECT * FROM tempTable ORDER BY tempTable.Name;
DROP TEMPORARY TABLE tempTable;
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

697 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question