Mysql
 sql >> डेटाबेस >  >> RDS >> Mysql

ईवेंट-कैलेंडर बनाने के लिए दो तालिकाओं से डेटा प्राप्त करें

अपडेट किया गया

SELECT user_id,
       COALESCE(MIN(CASE WHEN  1 BETWEEN DAY(start_date) AND DAY(end_date) THEN event_id END), 0) `1`,
       COALESCE(MIN(CASE WHEN  2 BETWEEN DAY(start_date) AND DAY(end_date) THEN event_id END), 0) `2`,
       COALESCE(MIN(CASE WHEN  3 BETWEEN DAY(start_date) AND DAY(end_date) THEN event_id END), 0) `3`,
       COALESCE(MIN(CASE WHEN  4 BETWEEN DAY(start_date) AND DAY(end_date) THEN event_id END), 0) `4`,
       COALESCE(MIN(CASE WHEN  5 BETWEEN DAY(start_date) AND DAY(end_date) THEN event_id END), 0) `5`,
       COALESCE(MIN(CASE WHEN  6 BETWEEN DAY(start_date) AND DAY(end_date) THEN event_id END), 0) `6`,
       COALESCE(MIN(CASE WHEN  7 BETWEEN DAY(start_date) AND DAY(end_date) THEN event_id END), 0) `7`,
       COALESCE(MIN(CASE WHEN  8 BETWEEN DAY(start_date) AND DAY(end_date) THEN event_id END), 0) `8`,
       COALESCE(MIN(CASE WHEN  9 BETWEEN DAY(start_date) AND DAY(end_date) THEN event_id END), 0) `9`,
       COALESCE(MIN(CASE WHEN 10 BETWEEN DAY(start_date) AND DAY(end_date) THEN event_id END), 0) `10`,
       COALESCE(MIN(CASE WHEN 11 BETWEEN DAY(start_date) AND DAY(end_date) THEN event_id END), 0) `11`,
       COALESCE(MIN(CASE WHEN 12 BETWEEN DAY(start_date) AND DAY(end_date) THEN event_id END), 0) `12`,
       COALESCE(MIN(CASE WHEN 13 BETWEEN DAY(start_date) AND DAY(end_date) THEN event_id END), 0) `13`,
       COALESCE(MIN(CASE WHEN 14 BETWEEN DAY(start_date) AND DAY(end_date) THEN event_id END), 0) `14`,
       COALESCE(MIN(CASE WHEN 15 BETWEEN DAY(start_date) AND DAY(end_date) THEN event_id END), 0) `15`
  FROM 
(
  SELECT id, event_id,
         CASE WHEN start_date < '2013-03-01' THEN '2013-03-01' ELSE start_date END start_date,
         CASE WHEN end_date   > '2013-03-31' THEN '2013-03-31' ELSE   end_date END end_date,
         user_id
    FROM events
   WHERE (start_date >= '2013-03-01' AND end_date <= '2013-03-31')
      OR (start_date <  '2013-03-01' AND end_date  > '2013-03-31')
      OR (start_date  < '2013-03-01' AND end_date BETWEEN '2013-03-01' AND '2013-03-31')
      OR (start_date BETWEEN '2013-03-01' AND '2013-03-31' AND end_date > '2013-03-31') 
) q
 GROUP BY user_id

आउटपुट:

| USER_ID | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
-----------------------------------------------------------------------------
|       1 | 0 | 1 | 1 | 3 | 0 | 0 | 0 | 0 | 0 |  0 |  0 |  0 |  0 |  0 |  0 |
|       2 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |  2 |  2 |  2 |  2 |  2 |  2 |
|       3 | 0 | 2 | 2 | 0 | 0 | 0 | 0 | 0 | 0 |  0 |  0 |  0 |  0 |  0 |  0 |

यह रहा SQLFiddle डेमो



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. मैसकल:डॉट के बाद दशमलव की एक निश्चित संख्या से अधिक वाली प्रत्येक पंक्तियों को कैसे प्राप्त करें?

  2. Mysqldump' को आंतरिक या बाहरी कमांड ऑपरेट करने योग्य प्रोग्राम या बैच फ़ाइल के रूप में मान्यता नहीं है

  3. विंडोज़ से Amazon EC2 पर MySQL से कैसे कनेक्ट करें?

  4. MySQL स्क्रिप्ट को H2 में बदलें

  5. Node.js mysql के साथ async/प्रतीक्षा का उपयोग करना