2

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 and now I want to order by date column but as both tables have date column so it's showing that date column twice I want it should be shown once and order by date here is an example of my tables:

Table 1 (sales_report)
date          agent_id  agent_name  agent_commission
01-Jan-2016   1         Jhon         200   
02-Jan-2016   2         Smith        250 
03-Jan-2016   3         Tracy        150
04-Jan-2016   4         Sam          120

Table 2 (payments)
date          agent_id  paid
02-Jan-2016   1         200   
03-Jan-2016   2         150 
04-Jan-2016   3         100
05-Jan-2016   4         50

I tried to join these both tables by agent_id now my question is how can I order by date ?

Here is the query I have:

SELECT *
FROM `sales_report`
INNER JOIN `payments`
ON `sales_report`.`agnt_id`=`payments`.`agnt_id` ORDER BY date
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • Which date do you want to order by? From sales_report or payments? – mmmm Aug 19 '16 at 11:52
  • As i understand you belongs right, you should use Union instead of join. But it will be clearer if you can Show us the expected result – Jens Aug 19 '16 at 11:53
  • Actually I just want to combine the results and order by date of both tables for example if table 1 date value is '01-Jan-2016' and table 2 date value is '02-Jan-2016' so system should print first table values and then values from 2nd table this was all the records will be appear – SimulationCode Aug 19 '16 at 11:58
  • Possible duplicate of [php/mysql multiple order by](http://stackoverflow.com/questions/38868833/php-mysql-multiple-order-by) – e4c5 Aug 19 '16 at 12:02
  • I just want to combine the records of two tables and order by date of each table. – SimulationCode Aug 19 '16 at 12:12
  • Actually union is the answer of my question but I can't use union because both tables have some different columns and I want to select all not only specific columns records – SimulationCode Aug 19 '16 at 12:20
  • 1
    This post solved my problem: http://stackoverflow.com/questions/2309943/unioning-two-tables-with-different-number-of-columns Anyway many thanks – SimulationCode Aug 19 '16 at 13:17

2 Answers2

2
    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.

ineersa
  • 3,445
  • 30
  • 40
0
    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.

MikyChow
  • 25
  • 1
  • 6
  • 2
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – rollstuhlfahrer Mar 21 '18 at 08:01