mysql join query order by two columns

前端 未结 2 1883
星月不相逢
星月不相逢 2021-01-21 16:45

I am trying to join two tables and in both tables I have 2 same column names like agent_id and date both tables have agent_id and date actually I join tables based on agent_id a

相关标签:
2条回答
  • 2021-01-21 17:14
        SELECT sr.*,pm.paid
        FROM `sales_report` as sr
        INNER JOIN `payments` as pm
        ON sr.`agent_id`=pm.`agent_id` 
        ORDER BY sr.date, pm.date
    

    Will get you date from first table and order by 1st table date then 2nd table date.

    0 讨论(0)
  • 2021-01-21 17:22
        SELECT * FROM
        (
             SELECT sr.date
                FROM `sales_report` as sr
             UNION
             SELECT pm.date
                FROM `payments` as pm
            )
        ORDER BY date
    

    I think this is what you wanted,Both sales_report and payments need be considered as a table.then sort it.

    0 讨论(0)
提交回复
热议问题