mysql通过两列联接查询顺序

mysql通过两列联接查询顺序

本文介绍了mysql通过两列联接查询顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试联接两个表,并且在两个表中我都有2个相同的列名,例如agent_id和date,两个表都具有agent_id和date,实际上我基于agent_id联接了表,现在我想按日期列进行排序,但是作为两个表有date列,因此它两次显示该date列,我希望它应该显示一次,然后按日期排序是我的表格示例:

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

我试图通过agent_id将这两个表连接起来,现在我的问题是如何按日期排序?

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

这是我的查询:

SELECT *
FROM `sales_report`
INNER JOIN `payments`
ON `sales_report`.`agnt_id`=`payments`.`agnt_id` ORDER BY date

推荐答案

    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.

这篇关于mysql通过两列联接查询顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 23:32