2

How do put this into one query?

For example:

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

Basically show 20 records of each day, month and year.

5
  • 1
    32 days in a month :-)? The obvious answer is to use a lot of unions. Commented Aug 4, 2012 at 11:01
  • I think you could probably come up with views that select the top 20 records (but I can't help but notice you're not ordering...) and select from that. Commented Aug 4, 2012 at 11:03
  • 1
    MySQL doesn't have functionality like row_number(). This makes it extremely hard to get 20 records per group instead of just 20 records. This means that writing each query independantly (and possibly using UNION to combine the results) really is the most straight forward option. Commented Aug 4, 2012 at 11:05
  • are you missing order by clause ? or you are taking any 20 recordds? Commented Aug 4, 2012 at 11:07
  • @JoeGJoseph taking any 20 records is fine.. Commented Aug 4, 2012 at 11:09

3 Answers 3

3

You can merge result set using UNION in this way :

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
Sign up to request clarification or add additional context in comments.

2 Comments

Where would you include order by submit_date desc?
'UNION ALL' does not remove duplicate which I would prefer 'UNION'
1

I think you need to use a stored function instead maybe something like that may help you

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

I do not have MySql installed now but you should try to create it from the console and call it. Good luck!

Comments

-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

Does this do, what you want?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.