What would be the fastest way to select 2 random rows from an SQL table ? (SQL only or not)
I am using MySQL on PhpMyAdmin.
Seems like it would be:
SELECT * FROM table LIMIT 2
What you will get is two rows in database default order.
Try this:
SELECT * FROM table ORDER BY RAND() LIMIT 0, 2
Related
I Have a problem with getting data from the database.
Actually, I want multiple data from the database with passing multiple same ids using IN in MySQL.
Like
table
SELECT * FROM table WHERE id IN (1,2,3,1,2)
I need result with five records.
OUTPUT like,
You can use LIMIT to limit the results to only 5 records and use ORDER BY FIELD to get the desired order. Here's a snippet:
SELECT * FROM table WHERE id IN (1,2,3) ORDER BY FIELD(id, 1, 2, 3) LIMIT 5;
I have a table with 8,000 Records, I want to select randomly some records (for example 10) from this table. These records have to be different from previously selected records.
For example with this query I select some questions:
SELECT coloumn1,column2 FROM `myTable` WHERE `status`=1 AND `group`=6 ORDER BY RAND() LIMIT 0, :max
Now how can I select new records randomly and different from previously selected records?
Simply you could store the id's of previously selected records and then add to your next query like below, to avoid selecting them twice:
WHERE id NOT IN = (list of ids)
Based on you'r example:
SELECT coloumn1,column2 FROM `myTable` WHERE `status`=1 AND `group`=6 AND id NOT IN (_IDS_) ORDER BY RAND() LIMIT 0
try :
SELECT coloumn1, column2 FROM myTable WHERE status=1 AND group=6
AND coloumn1 NOT IN (id,id,id,id,ids concatenated in php)
ORDER BY RAND() LIMIT 0, :max
and also in select, better select a column that is unique identifier for the row, and use that column with the NOT IN ()
avoid RAND() as its extremely slow - better work around it with php
If the number of selected records is too big, put them in a temp table, and use
NOT IN (SELECT id FROM temp table...)
I have query that gets 10 random posts , and as you know this is very slow and heavy query, is there any alternatives to submit this query without any slow appearance?
my current rand query :
SELECT * FROM posts ORDER BY RAND() LIMIT 10
From MySQL document:
SELECT * FROM tablename ORDER BY RAND() LIMIT 1
works for small tables, but once the tables grow larger than 300,000 records or so this will be very slow because MySQL will have to process ALL the entries from the table, order them randomly and then return the first row of the ordered result, and this sorting takes long time. Instead you can do it like this (atleast if you have an auto_increment PK):
SELECT MIN(id), MAX(id) FROM tablename;
Fetch the result into $a
//php code
$id=rand($a[0],$a[1]);
SELECT * FROM tablename WHERE id>='$id' LIMIT 1
I need help to write a MySQL query that would do the following for me:
It would select the last row from a certain column. Let's say the table name is 'mysite_codes' and the column name is 'code', there are many rows in this column, but I want the query to take the last added one and get my back one result in PHP (LIMIT 1 thing).
Would anyone please help me with this?
MySQL tables have no inherent sorting. If you want "the last added one" then you'll need an AUTO_INCREMENTing column like id.
Then you can write.
SELECT `code` FROM `mysite_codes` ORDER BY `id` DESC LIMIT 1
to get just the row with the highest id value.
Try this:
select code
from mysite_codes
order by add_date desc
limit 1
Assuming you have an auto-incremementing id column called something like "auto_id_column":
SELECT code FROM mysite_codes ORDER BY auto_id_column DESC LIMIT 0, 1;
How to fetch the first two rows from Mysql DB using Mysql PHP function? Is there any function which can give me first 2 or 3 rows from the select query we fired?
Use LIMIT. From the manual, to retrieve 3 rows:
SELECT * FROM tbl LIMIT 3;
Or to retrieve rows 6-15:
SELECT * FROM tbl LIMIT 5,10;
For this query (i.e. with no constraint) if you are not using an ORDER BY clause your results will be ordered as they appear in the database.
You can use the limit clause in your query:
select * from your_table limit 3
This will select the first three rows.
And:
select * from your_table limit 5, 3
The later will select rows starting from 5 and return three rows.
You have 2 options:
Fire a query to select all the rows
and then select 2 or 3 rows as
needed using PHP.
Fire a query to select only the
necessary number of rows using LIMIT
clause.
The 2nd option is preferable.