2

如何将其放入一个查询中?

例如:

SELECT * FROM `result` WHERE status = 'new' AND YEAR(`end_date`)  = '2012' limit 20
SELECT * FROM `result` WHERE status = 'new' AND YEAR(`end_date`)  = '2013' limit 20
SELECT * FROM `result` WHERE status = 'new' AND YEAR(`end_date`)  = '2014' limit 20
SELECT * FROM `result` WHERE status = 'new' AND YEAR(`end_date`)  = '2015' limit 20

SELECT * FROM `result` WHERE status = 'new' AND DAY(`end_date`)  = '1' limit 20
SELECT * FROM `result` WHERE status = 'new' AND DAY(`end_date`)  = '2' limit 20
... and to 31

and same for the Month Jan to Dec

基本上显示每天、每月和每年的 20 条记录。

4

3 回答 3

3

您可以通过这种方式使用UNION合并结果集:

SELECT * FROM `result` WHERE status = 'new' AND YEAR(`end_date`)  = '2012' limit 20
UNION
SELECT * FROM `result` WHERE status = 'new' AND YEAR(`end_date`)  = '2013' limit 20
UNION
SELECT * FROM `result` WHERE status = 'new' AND YEAR(`end_date`)  = '2014' limit 20
UNION
SELECT * FROM `result` WHERE status = 'new' AND YEAR(`end_date`)  = '2015' limit 20
UNION
SELECT * FROM `result` WHERE status = 'new' AND DAY(`end_date`)  = '1' limit 20
UNION 
SELECT * FROM `result` WHERE status = 'new' AND DAY(`end_date`)  = '2' limit 20
ORDER BY submit_date DESC
于 2012-08-04T11:40:43.690 回答
1

我认为您需要使用存储功能,而不是类似的东西可能会帮助您

create function MyRecords()
RETURNS @MyResultsTable table
BEGIN
    select * into @MyResultsTable from ? where ?
    select * into @MyResultsTable from ? where ?
    select * into @MyResultsTable from ? where ?
    select * into @MyResultsTable from ? where ?
    .....
    .....
    .....
end

我现在没有安装 MySql,但你应该尝试从控制台创建它并调用它。祝你好运!

于 2012-08-04T11:38:03.097 回答
-1
SELECT * FROM `result` WHERE status = 'new' AND YEAR(`end_date`)  >= '2012' AND YEAR(`end_date`) <= '2015' AND DAY(`end_date`)  >= '1' AND DAY(`end_date`)  <= '32' AND  MONTH(`end_date`)  >= '1' AND  MONTH(`end_date`)  <= '12'  limit 20

这样做,你想要什么?

于 2012-08-04T11:06:33.237 回答