mysql - 2选择1中的函数

时间:2011-09-25 17:30:09

标签: mysql

我想将2个选项合并为1,第一个选择是:

("SELECT * FROM `help_and_advice_forum` LIMIT 10, 10");

第二个是:

("SELECT * FROM WHERE `sticky` = '1' LIMIT 0, 5");

他们以任何方式选择前五个粘贴然后在10行之后选择10行吗?

由于

1 个答案:

答案 0 :(得分:2)

UNION他们在一起。 (假设help_and_advice_forum是您从第二个中省略的表名)

(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)

如果您有id之类的唯一ID列,请在其上设置ORDER BY

如果您需要从额外的10行中排除前五个sticky结果,否则表格中会有一些唯一的ID列,请尝试以下操作。我不肯定它会按预期工作。

(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)