给定 table1、table2 和 table3(假设它们的结构相同),是否可以选择 table1、table2 和 table3 的前 1/3 记录?那么第二个1/3呢?那么最后的1/3呢?
问问题
79 次
2 回答
0
第一个 1/3 记录:
SELECT *
FROM
(
SELECT *
FROM
(
SELECT * FROM Table1
UNION ALL
SELECT * FROM Table2
UNION ALL
SELECT * FROM Table3
UNION ALL
) sub
ORDER BY ID DESC
LIMIT 3
) t
ORDER BY Id ASC
LIMIT 1
第二个2/3::
SELECT *
FROM
(
SELECT *
FROM
(
SELECT * FROM Table1
UNION ALL
SELECT * FROM Table2
UNION ALL
SELECT * FROM Table3
UNION ALL
) sub
ORDER BY ID DESC
LIMIT 3
) t
ORDER BY Id ASC
LIMIT 2, 1
最后 1/3 记录:
SELECT *
FROM
(
SELECT *
FROM
(
SELECT * FROM Table1
UNION ALL
SELECT * FROM Table2
UNION ALL
SELECT * FROM Table3
UNION ALL
) sub
ORDER BY ID DESC
LIMIT 3
) t
ORDER BY Id DESC
LIMIT 1
于 2012-11-01T13:01:26.697 回答
0
尝试使用 LIMIT 和 OFFSET 子句,例如:
SELECT column FROM table1 LIMIT X OFFSET Y
union
SELECT column FROM table2 LIMIT X OFFSET Y+10
union
SELECT column FROM table3 LIMIT X OFFSET Y+20
但你必须参数化 LIMIT 和 OFFSET .. 这是一种方式
于 2012-11-01T13:01:35.803 回答