Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Select Random records then sort them.

Posted on 2007-04-02
6
Medium Priority
?
550 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 1500 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
What we learned in Webroot's webinar on multi-vector protection.
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

670 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