【问题标题】:sorting mixed table datas对混合表数据进行排序
【发布时间】:2018-10-17 21:27:58
【问题描述】:

我在 MySQL 数据库中有两个表。第一个表是contacts(customer, id),用于存储客户信息。第二个表history(report, nextFollowingDate, customerid) 存储客户联系的历史记录以及下一个后续日期。一个客户可以有多个nextFollowingDate 值不同的记录。

样本数据如下。

contacts表:

customer  id 
a          1
b          2
c          3

history表:

report     nextFollowingDate     customerid
report1     2018/04/23              1
report2     2018/04/25               1
report3     2018/04/22              2
report4     2018/04/26               3
report5     2018/05/30               2

我想按nextFollowingDate 的值按升序对联系人表中的客户进行排序。如下所示。

customer    nextFollow
1            2018/04/25
2            2018/05/30
3            2018/04/26

但我没有办法这样做。

【问题讨论】:

  • 你试过在mysql中执行吗?
  • 当您提出问题时,请尽量提供Minimal, Complete and Verifiable Example。如果你这样做,提供答案的发布者会发现他们很容易重现场景,并且答案会很有成效。对于这样的案例,您可以使用SQL Fiddle,这对于尝试回答的人来说会很容易。 :)

标签: mysql select sql-order-by


【解决方案1】:
SELECT customerId, MAX(nextFollowingDate) FROM history GROUP BY customerId

是您正在寻找的。但是,最后一个日期ASC到底是什么意思不是很清楚。我根据您的期望给出的示例结果集编写了这个答案。查找 this SQL Fiddle 以查看实际情况。

【讨论】:

    猜你喜欢
    • 2012-11-07
    • 2013-08-22
    • 1970-01-01
    • 1970-01-01
    • 2017-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    相关资源
    最近更新 更多