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
Solved

Select Random records then sort them.

Posted on 2007-04-02
6
544 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 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

APEX (Application Express) is used to develop a web application from Oracle. SQL Workshop is one of the tools that comes with Oracle APEX to query or modify the database objects or to make any changes to the structure.
Read about achieving the basic levels of HRIS security in the workplace.
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…

766 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