Solved

Select Random records then sort them.

Posted on 2007-04-02
6
537 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
  • 3
  • 2
6 Comments
 
LVL 142

Expert Comment

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

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 142

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
Creating and Managing Databases with phpMyAdmin in cPanel.
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now