I'm wonder if there is way to use LIMIT operator to always include first row in result set. I mean regardless of any range specified. For Ex:
SELECT * FROM `table` LIMIT 5, 10
should return first row and row 6 to 15 or:
SELECT * FROM `table` LIMIT 200, 30
should return first row and row 201 to 231. Is there a way to do that ?
Edit First row id is always 0
I don't see another way :
(SELECT * from table LIMIT 1)
UNION
(SELECT * FROM table LIMIT 5, 10)
or as Pointed by Scott, with new infos ;)
SELECT * from table Where id = 0
UNION
(SELECT * FROM table LIMIT 5, 10)
don't forget brackets :
To apply ORDER BY or LIMIT to an individual SELECT, place the clause
inside the parentheses that enclose the SELECT:
(SELECT a FROM t1 WHERE a=10 AND B=1 ORDER BY a LIMIT 10) UNION
(SELECT a FROM t2 WHERE a=11 AND B=2 ORDER BY a LIMIT 10);
You can use UNION to connect 2 selects: (SELECT * FROM table LIMIT 1) UNION (SELECT * FROM table LIMIT 5, 10)
Related
I am facing some unusual issue. Please help. There are 300 rows in a column. I want to display any 100 order by rand(). But within this randomly selected 100, there must be 2 rows. How can I write it?
Example:
"Select id from sample_table where id<300 or id>1 order by rand() limit 100"
But I want the result must include id=34 and id=78
Use UNION ALL to select the 2 rows that must exist in the results and 98 random rows:
select id from sample_table
where id in (34, 78)
union all
select id from (
select id from sample_table
where where id not in (34, 78)
order by rand() limit 98
) t
order by rand()
or simpler with conditional sorting:
select * from (
select id from sample_table
order by id not in (34, 78), rand()
limit 100
) t
order by rand()
Union both to the original SELCT and random the result of that union
SELECT
id
FROM
(SELECT 34 UNION SELECT 78 UNION SELECT
id
FROM
sample_table
WHERE
id < 300 OR id > 1
ORDER BY RAND()
LIMIT 98)
ORDER BY RAND()
I have a 2 statements with limits and union, but when I run the statement I only get the number of results that defined to the highest limit. I thought I would get X results from statement 1 plus Y results from statement 2. This did not occur. Why is that?
Here is the query example:
SELECT * FROM tableA WHERE X > 100 LIMIT 2
UNION
SELECT * FROM tableA WHERE X < 100 LIMIT 3;
The query above returns 3 results. However in this simple example I wanted 2 results + 3 results (5 results altogether).
If I modified the query like so I got the 5 results, but this query below appears misleading:
SELECT * FROM tableA WHERE X > 100 LIMIT 2
UNION
SELECT * FROM tableA WHERE X < 100 LIMIT 5;
I found that by wrapping each statement in another statement resolved this. Here is how the final statement appears:
SELECT * FROM(
SELECT * FROM tableA WHERE X > 100 LIMIT 2
)query1
UNION
SELECT * FROM(
SELECT * FROM tableA WHERE X < 100 LIMIT 3
)query2;
Thanks to anyone that can explain why behaviour of the initial two statements.
Unless parenthesis are used, the final ORDER BY and/or LIMIT clauses are applied to the query as a whole "outside/after" the union.
http://dev.mysql.com/doc/refman/5.7/en/union.html
From https://dev.mysql.com/doc/refman/5.7/en/union.html:
To apply ORDER BY or LIMIT to an individual SELECT, place the clause
inside the parentheses that enclose the SELECT:
(SELECT a FROM t1 WHERE a=10 AND B=1 ORDER BY a LIMIT 10)
UNION
(SELECT a FROM t2 WHERE a=11 AND B=2 ORDER BY a LIMIT 10);
Your LIMIT in your first example is applied to the entire outer query. I've added some parentheses to make what's happening implicitly more obvious.
(SELECT * FROM tableA WHERE X > 100 LIMIT 2
UNION
SELECT * FROM tableA WHERE X < 100)
LIMIT 3;
I have a table with multiple type values and I'm wanting to get a sample records from some of them.
My current query is as follows:
-- Pulling three sample records from each "type"
SELECT * FROM example WHERE type = "A" LIMIT 3
UNION ALL
SELECT * FROM example WHERE type = "B" LIMIT 3
UNION ALL
SELECT * FROM example WHERE type = "C" LIMIT 3
;
I expect this to return a total of 9 records; 3 from type = "A", 3 from type = "B", and 3 from type = "C".
However, the result that I actually receive is 3 records from type = "A" and nothing else.
I know for a fact that the other type values exist because I can run the individual SELECT statements and they return results.
Why is MySQL only returning 3 records and how can I have it return the full 9 records that I want?
I've created a SQL Fiddle to illustrate the issue: http://sqlfiddle.com/#!9/d911c/2
Use parentheses:
(SELECT * FROM example WHERE type = "A" LIMIT 3)
UNION ALL
(SELECT * FROM example WHERE type = "B" LIMIT 3)
UNION ALL
(SELECT * FROM example WHERE type = "C" LIMIT 3);
Demo here
This is documented in the manual:
To apply ORDER BY or LIMIT to an individual SELECT, place the clause
inside the parentheses that enclose the SELECT:
(SELECT a FROM t1 WHERE a=10 AND B=1 ORDER BY a LIMIT 10) UNION
(SELECT a FROM t2 WHERE a=11 AND B=2 ORDER BY a LIMIT 10);
Put parentheses around the selects
(SELECT * FROM example WHERE type = "A" LIMIT 3)
UNION ALL
(SELECT * FROM example WHERE type = "B" LIMIT 3)
UNION ALL
(SELECT * FROM example WHERE type = "C" LIMIT 3)
SQLFiddle
The limit is applied to all rows, not just each section. i.e. your setting a limit of 3 on all queries, not each section. So in order to apply a limit to each section you'd need to sub query it:
SELECT * FROM (select * from example WHERE type = "A" LIMIT 3) tbl1
UNION ALL
SELECT * FROM (select * from example WHERE type = "B" LIMIT 3) tbl2
UNION ALL
SELECT * FROM (select * from example WHERE type = "C" LIMIT 3) tbl3
;
That should give you the result you are looking for
How can I get a specific row (id=x) and then the reminders? Something like:
SELECT * FROM table ORDER BY id=5 FIRST THAN id DESC
I tried to use UNION ALL, like:
(SELECT * FROM table WHERE id=5)
UNION ALL
(SELECT * FROM table WHERE id!=5 ORDER BY id DESC)
but the result is unexpected since the second SELECT doesn't return the registers ordered by id (desc). In addition, in this way is neccessary to write much more.
SELECT * FROM table ORDER BY id = 5 DESC, id ASC
This will give you something like: 5, 1, 2, 3, 4, 6, 7, ...
You can put more than one clause on the order by line:
select *
from t
order by (case when id = 5 then 1 else 0 end) desc, id desc
Also, tables and result sets in SQL are unordered. The one exception is the use of order by for a result set. I wouldn't expect the union all method to work.
This will give you '5's first, then 'not 5's
SELECT *
FROM table
ORDER BY case when id=5 then 0 else 1 end ASC
How would I combine (stack vertically) the following 3 queries into one query that returns 100 rows, 50 rows from category 1, 25 from from category 2, 25 from category 3 all chosen randomly. I tried UNION but didn't seem to work.
select * from table where category_id = 1 order by rand() limit 50;
select * from table where category_id = 2 order by rand() limit 25;
select * from table where category_id = 3 order by rand() limit 25;
To apply ORDER BY or LIMIT to an individual SELECT, place the clause inside the parentheses that enclose the SELECT:
(select * from table where category_id = 1 order by rand() limit 50)
UNION ALL
(select * from table where category_id = 2 order by rand() limit 25)
UNION ALL
(select * from table where category_id = 3 order by rand() limit 25);
What you are looking for is the UNION ALL syntax (link is to MySQL documentation).
select * from table where category_id = 1 order by rand() limit 50
UNION ALL
select * from table where category_id = 2 order by rand() limit 25
UNION ALL
select * from table where category_id = 3 order by rand() limit 25;
edit: Semicolons removed, thanks #Matt Fenwick