根据id顺序加入两个表

时间:2015-01-07 08:57:36

标签: mysql jointable

我需要结合两个表并按两个表日期排序..

医学

| Id | Patientid | Name | Quantity |  medicine name | Takingtype | Epoch     |
===============================================================================
|  1 |       12  | raja | 20       | Ampicillin     |    1-1-1   | 1420446675|

外科

| Id | Patientid | Name | medicine name | Quantity | Epoch      |
=======================================================================
|  1 |       12  | raja | Mucinex oral  | 10       | 1420446675 |

任何人都可以帮助我吗? 结果应该是这样的,

|s.no| Date     | Name | Quantity  |  medicine name | Takingtype |
=================================================================
|  1 | 1-5-2015 | raja |  20       | Ampicillin     |    1-1-1   |
|  2 | 1-5-2015 | raja |  10       |Mucinex oral    |            |

请帮我加入表格,如上面显示的结果。我正在寻找一个mysql查询..

(我已经在php中更改了纪元时间。)

2 个答案:

答案 0 :(得分:1)

您是否在MySQL中寻找 UNION

SELECT name, quantity, medicine, Takingtype FROM Medicine
UNION ALL
SELECT name, quantity, medicine, NULL FROM Surgical

答案 1 :(得分:1)

如果我理解你的问题,那么你想要的不是一个联盟,而是一个联盟。

select * from (
    select Name, Quantity, MedicineName, Epoch, Takingtype From Medicine
        union all 
    select Name, Quantity, MedicineName, Epoch, NULL From Surgical
) x order by epoch;

我还假设您希望s.no成为包含行序号的列。据我所知,如果没有临时表,你无法在MySQL中实现这一点(专家可能会在这里纠正我。)

最后,如果您的日期时间确实是时代的一个时间,那么您需要使用FROM_UNIXTIMESTAMP内置函数,

select * from (
    select FROM_UNIXTIMESTAMP(Epoch) AS Date, Name, Quantity, MedicineName, Epoch, Takingtype From Medicine 
        union all 
    select FROM_UNIXTIMESTAMP(Epoch) AS Date, Name, Quantity, MedicineName, Epoch, NULL From Surgical
) x order by epoch;