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)>
Related
Suppose I have the following MySQL select statements:
SELECT * FROM tableX WHERE y_column > 10 AND Z = 200 LIMIT 100;
and
SELECT * FROM tableX WHERE Z = 200 y_column <= 10 LIMIT 100;
Suppose also that it is possible that I have more than 100 records in tableX that return a match for each statement. I want to execute the first statement before the second statement and have both of the statements return a result set that has a size <= 100.
Is it possible to write this in one MySQL statement?
What I went with was something like the following:
SELECT * FROM tableX WHERE ORDER BY y_column DESC LIMIT 100;
Not really sure how to do this, but is it possible in one query to fetch x amount of records from a table, and if not enough is found, it will just randomly select duplicates.
I have a photos table, let's say it has 5 records in it, and I want to pull out 10 records and order them randomly, so I have something like:
SELECT * FROM TABLE
ORDER BY RAND()
LIMIT 10
This will just pull back 5 randomly, cos that is all I have in the table. Can I tell MySQL, hey, if you find less than 10, just randomly grab more until you reach that number?
Any help appreciated!
Thanks
This will do it:
select * from Table1
union all
select * from
(
select * from
(
select * from Table1 limit 10
union all
select * from Table1 limit 10
union all
select * from Table1 limit 10
union all
select * from Table1 limit 10
-- more unions...
) t2 order by rand()
) rand_ordered
limit 10
Union the table for as many times as your number of needed records is (10 times in this example) to make it work with only one row in the table, order the result by rand() and append it to your table with another union all.
This might not be the best performing solution tho, but it will do it.
Example here: SQLFIDDLE
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;
I know MySQL doesn't have a TOP 1 function for selecting the the first row. So I use limit 1 to do it. But how can I select the first row from two tables (tableX, tableY) at the same time.
like this?
SELECT * FROM Database.tableX
limit 1, Database.tableY limit 1
Probably, if the table definitions match:
SELECT * FROM Database.tableX LIMIT 1
UNION ALL
(
SELECT * FROM Database.tableY LIMIT 1
)
Note the inner bracket is necessary. Else the optimizer will limit the result set to 1 seeing the last LIMIT 1 clause
This is not possible unless there is some kind of relation between the two rows.
The other answers here try to join two rows, but they give no guarantee that it is the first row in each table. They also assume there is a field that can be matched from the two tables.
My best suggestion is to do two queries:
SELECT * FROM Database.tableX limit 1;
SELECT * FROM Database.tableY limit 1;
EDIT: you should also add a ORDER BY to tell MySQL how to sort the rows.
Just this:
SELECT *
FROM Database.TableX, Database.TableY
LIMIT 1
or maybe this?
SELECT *
FROM
(SELECT * FROM Database.TableX LIMIT 1) t1,
(SELECT * FROM Database.TableY LIMIT 1) t2
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.