2

これを1つのクエリにどのように配置しますか?

例えば:

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 に答える