I have to create a listing in random order from a few mysql tables.
I can't use ORDER BY RAND() for a few reasons:
- The random order has to change only every 24hs, ie: today it will show rows number 4 2 3 1 and tomorrow 2 3 1 4, but it should be the same all day long.
- I'm dividing the results in pages using LIMIT x, y.
I suppose I would have to save the random order in a mysql table, but how can I order the results based on that table?
Any ideas?
2 2589 kovik 1,044
Recognized Expert Top Contributor
The only way to (consistently) order randomly in MySQL is ORDER BY RAND().
For your situation, you'll want to random select the rows, then save them for that particular day. For example: - create table `random` (`date` date, `id` int, primary key(`date`));
- $query = "SELECT `column` FROM `table` LEFT JOIN (`random`) ON (`random`.`id` = `table`.`id`) WHERE `date` = '" . date('Y-m-d') . "';";
-
$result = mysql_query($query);
-
-
if(!mysql_num_rows($result))
-
{
-
mysql_query("DELETE FROM `random`;");
-
$result = mysql_query("SELECT `id` FROM `table` ORDER BY `rand` LIMIT 10;");
-
while($data = mysql_fetch_object($result))
-
{
-
mysql_query("INSERT INTO `random` SET `id` = " . (int)$data->id . ", `date` = '" . date('Y-m-d') . "';");
-
}
-
-
$result = mysql_query($query);
-
}
This isn't tested at all, but in theory, it should be fine. It's not exactly the kind of code that I'd use because I like to condense these basic queries into much more complex queries, but for readability's sake, I've simplified it.
Thanks a lot!
That's what I was thinking of doing, but I thought there may be some way to do it without storing it in another table.
I'll test it next week and come back if anything goes wrong :P
Thanks again!
Sign in to post your reply or Sign up for a free account.
Similar topics |
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 = 'Y' ORDER BY RAND() LIMIT
5
|
by: Frank & Janny Plaza |
last post by:
I am trying to write a program in which the users will each enter their names and when all names have been entered, I want to randomly sort this list of names several times and then show the order in which they have beeen sorted. I tried using sort(), but this only sorts the names in the order in which they are entered. Any suggestions?
Thanks, Cisco
|
by: Bart Nessux |
last post by:
New to Python... trying to figure out how to count the objects in a list
and then map the count to the objects or convert the list to a dict... I
think the latter would be better as I need a number associated with each
entry. Any pointers?
Also, does this bit of code look to be truely random?
def random_number_gen():
winner =
winner.append(random.sample(xrange(100000), 1))
|
by: Jean-Francois.Doyon |
last post by:
Hello,
I'm trying to retrieve a limited number of random rows, and order them by a
column, and am not having any luck with that last part:
SELECT * FROM tablename ORDER BY random(), id LIMIT 10
Returns everything more or less as expected, except for the fact that the
results aren't sorted by "id" ...
|
by: Nhmiller |
last post by:
Is there a way to do this? Thanks.
Neil
Cat Paintings At Carol Wilson Gallery
http://www.carolwilsongallery.com
| |
by: Ben Pfaff |
last post by:
One issue that comes up fairly often around here is the poor
quality of the pseudo-random number generators supplied with many
C implementations. As a result, we have to recommend things like
using the high-order bits returned by rand() instead of the
low-order bits, avoiding using rand() for anything that wants
decently random numbers, not using rand() if you want more than
approx. UINT_MAX total different sequences, and so on.
So I...
|
by: gl |
last post by:
How do I take an array or arraylist, and sort it randomly? Like suppose the
items in it are (1,2,3,4,5) and I want to get it to be in a random order
(2,3,1,4,5). How do you do that? I think it's a call to the array or array
lists sort method, but i'm not exactly sure how you do it.
|
by: Arnau Rebassa |
last post by:
Hi everybody,
I'm doing the following query:
select * from messages order by random() limit 1;
in the table messages I have more than 200 messages and a lot of times, the
message retrieved is the same. Anybody knows how I could do a more "random"
random?
|
by: Boris Borcic |
last post by:
does
x.sort(cmp = lambda x,y : cmp(random.random(),0.5))
pick a random shuffle of x with uniform distribution ?
Intuitively, assuming list.sort() does a minimal number of comparisons to
achieve the sort, I'd say the answer is yes. But I don't feel quite confortable
with the intuition... can anyone think of a more solid argumentation ?
|
by: badcrusher10 |
last post by:
Hello.
I'm having trouble figuring out what to do and how to do.. could someone explain to me what I need to do in order to work?
THIS IS WHAT I NEED TO DO:
Professor Snoop wants a program that will randomly generate 10 unique random numbers. Your job is to write a program that produces random permutations of the numbers 1 to 10. “Permutation” is a mathematical name for an arrangement. For example, there are six permutations of the...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |