Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. how to join all three query results to one in mysql

how to join all three query results to one in mysql

Scheduled Pinned Locked Moved Database
databasemysqlregextutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sr159
    wrote on last edited by
    #1

    hi i have written 3 different select query in mysql based on yesterday,today, tommorow dates to get the birthday details as follows select concat(name,', ',title) as 'Yesterday 30-04-2014' from personal_details where DOB=curdate() - interval 1 day select concat(name,', ',title) as 'Today 01-05-2014' from personal_details where DOB=DATE(NOW()) select concat(name,', ',title) as 'Tommorow 02-05-2014' from personal_details where DOB=curdate() + interval 1 day how to join all 3 query results to one, i need to show as below Yesterday Today Tomorrow aaaa gggg nnnnn bbbb hhhhh mmmm dddd jjjjj eeee ffff How to achieve this, i am not able to do. If i tried with join there it wont match any condition and will not work because in each query result i will get unique values. I tried with another query by putting as below select a.* from (select CASE DOB WHEN (curdate() - interval 1 day) THEN concat(name,', ',title) ELSE '' END AS 'Yesterday', CASE DOB WHEN (DATE(NOW())) THEN concat(name,', ',title) ELSE '' END AS 'Today',CASE DOB WHEN (curdate() + interval 1 day) THEN concat(name,', ',title) ELSE '' END AS 'Tomorrow' from personal_details ) as a where a.Yesterday IS NOT NULL and a.Today IS NOT NULL and a.Tomorrow IS NOT NULL It is showing the result like in all the three column some rows will be null I dont want null values in any column. i want out as above. How to achieve this. If anybody knows please reply me. Thanks in advance.

    S 1 Reply Last reply
    0
    • S sr159

      hi i have written 3 different select query in mysql based on yesterday,today, tommorow dates to get the birthday details as follows select concat(name,', ',title) as 'Yesterday 30-04-2014' from personal_details where DOB=curdate() - interval 1 day select concat(name,', ',title) as 'Today 01-05-2014' from personal_details where DOB=DATE(NOW()) select concat(name,', ',title) as 'Tommorow 02-05-2014' from personal_details where DOB=curdate() + interval 1 day how to join all 3 query results to one, i need to show as below Yesterday Today Tomorrow aaaa gggg nnnnn bbbb hhhhh mmmm dddd jjjjj eeee ffff How to achieve this, i am not able to do. If i tried with join there it wont match any condition and will not work because in each query result i will get unique values. I tried with another query by putting as below select a.* from (select CASE DOB WHEN (curdate() - interval 1 day) THEN concat(name,', ',title) ELSE '' END AS 'Yesterday', CASE DOB WHEN (DATE(NOW())) THEN concat(name,', ',title) ELSE '' END AS 'Today',CASE DOB WHEN (curdate() + interval 1 day) THEN concat(name,', ',title) ELSE '' END AS 'Tomorrow' from personal_details ) as a where a.Yesterday IS NOT NULL and a.Today IS NOT NULL and a.Tomorrow IS NOT NULL It is showing the result like in all the three column some rows will be null I dont want null values in any column. i want out as above. How to achieve this. If anybody knows please reply me. Thanks in advance.

      S Offline
      S Offline
      Simon_Whale
      wrote on last edited by
      #2

      Firstly to add your 3 queries together to act as a single result set you should look into the UNION[^] keyword. To organise the layout so that the column headers become Yesterday, Today and Tomorrow you need to look into PIVOT[^] this example should show you how to create one.

      Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

      S 1 Reply Last reply
      0
      • S Simon_Whale

        Firstly to add your 3 queries together to act as a single result set you should look into the UNION[^] keyword. To organise the layout so that the column headers become Yesterday, Today and Tomorrow you need to look into PIVOT[^] this example should show you how to create one.

        Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

        S Offline
        S Offline
        sr159
        wrote on last edited by
        #3

        if i use UNION i will get one column result. I should get 3 column but without null values. How to achieve this

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups