简体   繁体   English

连接两个表,然后按日期排序,但将两个表合并

[英]Join two tables, then Order By date, BUT combining both tables

Alright, I'm trying to figure out why I can't understand how to do this well... 好吧,我试图弄清楚为什么我不明白如何做到这一点...

I have two tables: 我有两个表:

  • invoices : 发票

    • id ID
    • userID 用户身份
    • amount
    • date 日期
  • payments : 付款方式

    • id ID
    • userID 用户身份
    • amount
    • date 日期

So, the goal here is to join both tables, where the userID matches whatever I want it to be - and then return everything ordered by date (most recent at the top). 因此,这里的目标是连接两个表,其中userID匹配我想要的任何内容-然后返回按日期排序的所有内容(最新的在顶部)。 However, because there is a date field in each of the tables, I'm not sure how MySQL will handle things... will is sort by both dates automatically? 但是,由于每个表中都有一个日期字段,所以我不确定MySQL将如何处理事情……会自动按两个日期排序吗? Here's what I was thinking... 这就是我在想...

"SELECT DISTINCT *
            FROM invoices,payments
            WHERE {$userID} = invoice.userID
            OR {$userID} = payments.userID
            ORDER BY date DESC";

But, it's starting to become clear to me that maybe this isn't even the right use of a join command... maybe I need to just get all data on each table alone, then try to sort it somehow with PHP? 但是,对我来说,开始变得很清楚,也许这甚至不是对join命令的正确使用……也许我只需要单独获取每个表上的所有数据,然后尝试使用PHP对其进行排序? If that's the better method, what's a good way to do this type of DATE sort while keeping all row data in tact? 如果那是更好的方法,那么在保持所有行数据不变的情况下进行这种DATE排序的好方法是什么?

I should add, the TIME inside the unix timestamp (that's how "date" is stored) is NOT negligible - it should sort by the date and time. 我应该补充一下,unix时间戳(即“日期”的存储方式)内的TIME不可忽略-它应按日期和时间排序。

Thanks all... 谢谢大家

If the columns of both tables are the same, you can use a UNION 如果两个表的列相同,则可以使用UNION

SELECT X.*
  FROM ( SELECT `id`,
                `userID`,
                 'INVOICE' AS PTYPE
                 `amount`,
                 `date`
            FROM `invoices`
           WHERE {$userID} = userID  
          UNION
          SELECT `id`,
                 `userID`,
                 'PAYMENT' AS PTYPE
                 `amount`,
                 `date`
            FROM `payments`
           WHERE {$userID} = userID  
        ) X
 ORDER BY X.`date`

EDIT 编辑

Read the relevant section of the MySQL manual on UNIONS. 阅读有关UNIONS的MySQL手册的相关部分。 There are other ways of phrasing this, but this is my preferred style - it should be clear to anybody reading that the ORDER BY clause applies to the result of both sides of the UNION. 有这种措辞的其他方式,但是这是我喜欢的风格-它应该是明确的任何人读取ORDER BY子句适用于UNION 两侧的结果。 A carelessly written UNION - even with an ORDER BY - may still leave the final resultset in indeterminate order. 粗心编写的UNION-即使使用ORDER BY-仍可能使最终结果集的顺序不确定。

The purpose of the PTYPE is that this query returns an extra column called PTYPE, that indicates whether each individual row is an INVOICE or a PAYMENT... ie. PTYPE的目的是该查询返回一个称为PTYPE的额外列,该列指示每一行是INVOICE还是PAYMENT...。 which of the two tables it comes from. 它来自两个表中的哪个。 It's not mandatory, but can often be useful within a union 它不是强制性的,但在工会中通常很有用

Because you have two identical fields named date, MySQL will not know which one you're trying to order by. 因为您有两个相同的名为date的字段,所以MySQL将不知道您要按哪个字段排序。

"SELECT DISTINCT *
            FROM invoices,payments
            WHERE {$userID} = invoice.userID
            OR {$userID} = payments.userID
            ORDER BY invoices.date, payments.date DESC";

This would sort on the invoice date, then the payment date - if that's what you are trying to find out 这将按发票日期排序,然后按付款日期排序(如果您要查找的是该日期)

If your data tipe is Date , Timestamp , or anything related, the SGBD will order it properly. 如果您的数据提示是DateTimestamp或任何相关内容,则SGBD会正确排序。 If that was what you've asked. 如果那是您的要求。

But if the datatype is String, even when dates is store, it will not sort the way you want. 但是,如果数据类型为String,那么即使存储日期,它也不会按照您想要的方式排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM