0

I've two different queries like

// This query will return past month records
SELECT b.list_id, r.name, r.city, FROM_UNIXTIME(b.bookingdate, '%Y-%m-%d- %h:%i:%s') AS 'bookingdate', COUNT(*) AS bookings
FROM booking b JOIN restaurants r ON r.list_id = b.list_id
WHERE b.bookingdate BETWEEN 1383224400 AND 1385730000 GROUP BY b.list_id

// This query will return current month records
SELECT b.list_id, r.name, r.city, FROM_UNIXTIME(b.bookingdate, '%Y-%m-%d- %h:%i:%s') AS 'bookingdate', COUNT(*) AS bookings
FROM booking b JOIN restaurants r ON r.list_id = b.list_id
WHERE b.bookingdate BETWEEN 1385816400 AND 1388408400 GROUP BY b.list_id

Result fields for both queries list_id, name, city, bookingdate, bookings

I want to combine both query results into one and want result fields like list_id, name, city, bookingdate, bookings_lastmonth, bookings_currentmonth

What is the best way to do this?

1
  • JOIN has special meaning in SQL, using that in your title makes this confusing. Commented Dec 10, 2013 at 5:39

4 Answers 4

3

Use UNION property:-

SELECT b.list_id, r.name, r.city, FROM_UNIXTIME(b.bookingdate, '%Y-%m-%d- %h:%i:%s') AS 'bookingpreviousdate', '0' AS bookingnextdate, COUNT(*) AS bookings
FROM booking b JOIN restaurants r ON r.list_id = b.list_id
WHERE b.bookingdate BETWEEN 1383224400 AND 1385730000 GROUP BY b.list_id
UNION
SELECT b.list_id, r.name, r.city, '0' AS 'bookingpreviousdate', 'FROM_UNIXTIME(b.bookingdate, '%Y-%m-%d- %h:%i:%s')' AS bookingnextdate, COUNT(*) AS bookings
FROM booking b JOIN restaurants r ON r.list_id = b.list_id
WHERE b.bookingdate BETWEEN 1385816400 AND 1388408400 GROUP BY b.list_id
Sign up to request clarification or add additional context in comments.

3 Comments

I want to combine both query results into one and want result fields like list_id, name, city, bookingdate, bookings_lastmonth, bookings_currentmonth UNION will give results of same fields like list_id, name, city, bookingdate, bookings inshort, I want result field "bookings" in two different columns bookings_lastmonth, bookings_currentmonth
then add one more column for dates.
for 'previousmonthdates' value will be not equal to 0, and for 'nextmonthdates' value will be not equal to 0.
0

use the union operator, it'll put two queries into one table

http://dev.mysql.com/doc/refman/5.0/en/union.html

Comments

0

I am not sure about the bookingdate

SELECT b.list_id, r.name, r.city, FROM_UNIXTIME(b.bookingdate, '%Y-%m-%d- %h:%i:%s') AS bookingdate, 
  COUNT(b.list_id) AS bookings_lastmonth, COUNT(c.list_id) AS bookings_currentmonth
FROM restaurants r 
LEFT JOIN booking b ON r.list_id = b.list_id AND b.bookingdate BETWEEN 1383224400 AND 1385730000 
LEFT JOIN booking c ON r.list_id = c.list_id AND c.bookingdate BETWEEN 1385816400 AND 1388408400
GROUP BY r.list_id
HAVING bookings_lastmonth>0 OR bookings_currentmonth>0

Comments

0

Somehow its worked by this query

SELECT t1.list_id, t1.name, t1.city, t1.bookings AS bookings_lastmonth, t2.bookings AS bookings_currentmonth 
FROM   (SELECT b.list_id, r.name, r.city, FROM_UNIXTIME(b.bookingdate, '%Y-%m-%d- %h:%i:%s') AS 'bookingdate', COUNT(*) AS bookings 
        FROM   booking b JOIN restaurants r ON r.list_id = b.list_id 
        WHERE  b.bookingdate BETWEEN 1383224400 AND 1385730000 
        GROUP  BY b.list_id) AS t1

        JOIN (SELECT b.list_id, r.name, r.city, FROM_UNIXTIME(b.bookingdate, '%Y-%m-%d- %h:%i:%s') AS 'bookingdate', COUNT(*) AS bookings 
        FROM   booking b JOIN restaurants r 
        ON r.list_id = b.list_id 
        WHERE  b.bookingdate BETWEEN 1385816400 AND 1388408400 
        GROUP  BY b.list_id) AS t2 

        ON t1.list_id = t2.list_id 

but still I have to work for more imporovements

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.