I need to take 10 rows from table. It has to be random but in that table I have only 5 rows.
And so if I select:
SELECT * FROM `names` order by rand() limit 10
But this query returns me only 5.
How to get five more with repeat?
SELECT m.* FROM names m,names n order by rand() limit 10
This may be what you wanted
Try like
SELECT * FROM names
UNION ALL
SELECT * FROM names
ORDER BY RAND() LIMIT 10;
Related
I have met one problem that required me to run the query to select the data 10 records randomly among 100 records(limit 100) which total records of my table is up to 9000 records.
Do you guys have any ideas on this problem?.
Fetch 100 rows and get random 10 rows using this query.
MySQL
Modified Solution
An embedded select with 100 limit and a main select with random 10 limit.
SELECT * FROM
(SELECT * FROM employee LIMIT 100) `a`
ORDER BY RAND() LIMIT 10;
Or you can create a table where you can store the first 100 rows and select random 10 rows from it.
Like INSERT INTO tbl2 SELECT * FROM tbl1 LIMIT 100;.
Then SELECT * FROM tbl2 ORDER BY RAND() LIMIT 10;.
OLD Solution
SELECT * FROM tbl1 a
LEFT JOIN tbl1 b ON a.`col1`=b.`col1`
LIMIT 100, 10;
I want to select 5 random rows from a table but only from the 20 most recent rows. I know the 2 statements separately would be something like:
SELECT * FROM table ORDER BY RAND() LIMIT 5
SELECT * FROM table ORDER BY date DESC LIMIT 20
How would I combine these 2 statements so it will select 5 random rows from the 20 most recent rows? Thanks.
Use an nested select
SELECT foo.* FROM (SELECT * FROM table ORDER BY date DESC LIMIT 20 ) as foo
ORDER BY RAND() LIMIT 5
Simply nest them:
SELECT * FROM (
SELECT * FROM table ORDER BY date DESC LIMIT 20
) ORDER BY RAND() LIMIT 5
Look up subqueries!
SELECT d.* FROM (SELECT * FROM table ORDER BY date DESC LIMIT 20) as d ORDER BY RAND() LIMIT 5;
Lets say I have a 1000 rows in my table.
I want to select 10 of those at random.
SELECT * FROM table ORDER BY RAND() LIMIT 10
Then I want to select the row in that result with the highest value for number
SELECT * FROM table ORDER BY number DESC LIMIT 1
Can anyone help me come up with an efficient way of doing this?
Just use a subquery:
SELECT *
FROM (
SELECT * FROM table ORDER BY RAND() LIMIT 10
)
ORDER BY number DESC LIMIT 1
From a simple SQL query in MySQL how can I get only the 5 first results?
And then, how can I get the next 5 results?
For example (pseudo code):
select * from (select * from some_table) where <first 5 results>
select * from (select * from some_table) where <second 5 results (6-10)>
You should be able to get the first 5 results with a LIMIT 5 at the end of your statement:
SELECT * FROM some_table LIMIT 5;
And then you can get results 6-10 with a query like this:
SELECT * FROM some_table LIMIT 5 OFFSET 5;
As another example, you could get results 6-15 with a query like this:
SELECT * FROM some_table LIMIT 10 OFFSET 5;
Please keep in mind that, if you don't add an ORDER BY statement, the results are retrieved in arbitrary order. Consequently, it doesn't really make sense to use LIMIT and OFFSET in the absence of an ORDER BY.
You can do this by making a SQL union
select * from (select * from some_table) where <first 5 results>
UNION ALL
select * from (select * from some_table) where <second 5 results (6-10)>
how to form a query to select 'm' rows randomly from a query result which has 'n' rows.
for ex; 5 rows from a query result which has 50 rows
i try like as follows but it errors
select * from (select * from emp where alphabet='A' order by sal desc) order by rand() limit 5;
u can wonder that why he needs sub query, i need 5 different names from a set of top 50 resulted by inner query.
SELECT * FROM t
ORDER BY RAND() LIMIT 5
or from your query result:
SELECT * FROM ( SELECT * FROM t WHERE x=y ) tt
ORDER BY RAND() LIMIT 5
This will give you the number to use as 'm' (limit)
TRUNCATE((RAND()*50),0);
...substitute 50 with n.
To check it try the following:
SELECT TRUNCATE((RAND()*50),0);
I should warn that this could return 0 as a result, is this ok for you?
For example you could do something like this:
SELECT COUNT(*) FROM YOUR_TABLE
...and store the result in a variable named totalRows for example. Then you could do:
SELECT * FROM YOUR_TABLE LIMIT TRUNCATE((RAND()*?),0);
where you substitute the '?' with the totalRows variable, according to the tech stack you are using.
Is it clearer now? If not please add more information to your question.