如何在每个最新日期中选择适当的行?

时间:2018-10-18 13:17:12

标签: mysqli

我只想在表中记录的每个最新date_中获得适当的行。

这是我下面的示例数据。

| number | landing_id | percentage |   date_recorded    |
+---------------------+------------+--------------------+
|   1    |     1      |     19     |2018-10-02 21:42:22 |
|   2    |     1      |     44     |2018-10-09 15:43:32 |
|   3    |    null    |    null    |        null        |
|   4    |     3      |     22     |2018-10-13 15:43:56 |
|   5    |     3      |     54     |2018-10-16 18:44:03 |
|   6    |     3      |     33     |2018-10-18 20:33:24 |
|   7    |    null    |    null    |        null        |
|   8    |     5      |     13     |2018-10-15 19:43:49 |
+---------------------+------------+--------------------+

我的查询代码。

SELECT
t1.landing_id,
t1.id_number,
t2.percentage,
MAX(t2.date_recorded)
FROM fish_landing AS t1
LEFT OUTER JOIN percentage AS t2
  ON t1.landing_id = t2.landing_id 
        AND t2.date_recorded < NOW()
GROUP BY t1.landing_id

这是上面查询代码的结果,该百分比列与最新的date_recorded不匹配。

|  number  | landing_id | percentage |   date_recorded    |
+----------+------------+------------+--------------------+
|    1     |     1      |     19     |2018-10-09 15:43:32 |
|    2     |    null    |    null    |        null        |
|    3     |     3      |     22     |2018-10-18 20:33:24 |
|    4     |    null    |    null    |        null        |
|    5     |     5      |     33     |2018-10-15 19:43:49 |
|    6     |    null    |    null    |        null        |
+----------+------------+------------+--------------------+

我要显示的输出数据表是下表,具有从最新date_recorded开始的适当列百分比。

|  number  | landing_id | percentage |   date_recorded    |
+----------+------------+------------+--------------------+
|    1     |     1      |     44     |2018-10-09 15:43:32 |
|    2     |    null    |    null    |        null        |
|    3     |     3      |     13     |2018-10-18 20:33:24 |
|    4     |    null    |    null    |        null        |
|    5     |     5      |     54     |2018-10-15 19:43:49 |
|    6     |    null    |    null    |        null        |
+----------+------------+------------+--------------------+

我希望有人可以帮助我解决我的问题。

0 个答案:

没有答案