mysql - 2 select functions in 1
I want to combine 2 selects into 1 the first one is:
("SELECT * FROM `help_and_advice_forum` LIMIT 10, 10");
The second is:
("SELECT * FROM WHERE `sticky` = '1' LIMIT 0, 5");
Is their any way to select 开发者_如何学Cthe first five sticky's then select 10 rows after 10 rows?
Thanks
UNION
them together. (assuming help_and_advice_forum
is the table name you omitted from the second one)
(SELECT * FROM `help_and_advice_forum` WHERE `sticky` = '1' ORDER BY id LIMIT 0, 5)
UNION
(SELECT * FROM `help_and_advice_forum` ORDER BY id LIMIT 10, 10)
If you have some unique id column like id
, set an ORDER BY
on it.
If you need to exclude the first five sticky
results from your additional 10 rows where they might otherwise overlap and there is some unique id column in the table, try something like the following. I'm not positive it will work as intended though.
(SELECT * FROM `help_and_advice_forum` WHERE `sticky` = '1' ORDER BY id LIMIT 0, 5)
UNION
(SELECT * FROM `help_and_advice_forum` WHERE id NOT IN (SELECT id FROM help_and_advice_forum WHERE sticky=1 ORDER BY id LIMIT 0, 5) LIMIT 10, 10)
精彩评论