MySQL如何选择获取特定状态,如果不满足要求状态,还可以获取最后一个ID?

时间:2016-10-04 03:35:51

标签: mysql sql

我在其他帖子上看到了类似的解决方案,但我在将问题应用到我的问题上时遇到了问题。

我有这张代金券的历史记录:

int getBalance() const // <---- {return bal;}

t_release

+------------+---------+----------------+-------------------+ | release_id | code_id | code_status_id | code_created_date | +------------+---------+----------------+-------------------+ | 1 | 32 | 2 | 4/28/2016 8:54 | | 1 | 32 | 2 | 4/28/2016 8:54 | | 2 | 32 | 3 | 4/28/2016 8:55 | | 3710 | 32 | 2 | 6/18/2016 10:20 | | 4 | 33 | 2 | 4/28/2016 9:54 | | 5 | 33 | 2 | 4/28/2016 10:54 | | 3711 | 33 | 2 | 6/18/2016 11:20 | | 6 | 34 | 2 | 4/28/2016 11:54 | | 7 | 34 | 3 | 4/28/2016 0:54 | | 3712 | 34 | 2 | 6/18/2016 0:20 | +------------+---------+----------------+-------------------+

r_code_status

当我跑步时:

+----------------+-------------+
| code_status_id | code_status |
+----------------+-------------+
|              1 | Available   |
|              2 | Requesting  |
|              3 | Paid        |
+----------------+-------------+

SELECT t1.release_id, t1.code_id, t1.code_status_id, t1.code_created_date FROM t_release t1 LEFT JOIN t_release t2 ON t1.code_id = t2.code_id AND t1.release_id < t2.release_id WHERE ISNULL(t2.release_id)

我需要code_id符合+------------+---------+----------------+-------------------+ | release_id | code_id | code_status_id | code_created_date | +------------+---------+----------------+-------------------+ | 3710 | 32 | 2 | 6/18/2016 10:20 | | 3711 | 33 | 2 | 6/18/2016 11:20 | | 3712 | 34 | 2 | 6/18/2016 0:20 | +------------+---------+----------------+-------------------+ 或'付费',查询可以将其检索为上次历史记录,否则code_status_id = '3' code_id,然后检索到最后code_status_id = '2'。< / p>

我想要这样的结果:

id(release_id)

DDL:

+------------+---------+----------------+-------------------+
| release_id | code_id | code_status_id | code_created_date |
+------------+---------+----------------+-------------------+
|       2    |      32 |              3 | 4/28/2016 08:55   |
|       3711 |      33 |              2 | 6/18/2016 11:20   |
|       7    |      34 |              3 | 4/28/2016 0:54    |
+------------+---------+----------------+-------------------+

这是链接sqlfiddle:http://sqlfiddle.com/#!9/87843

2 个答案:

答案 0 :(得分:1)

SELECT * FROM t_release t where release_id IN 
    (SELECT DISTINCT release_id FROM t_release WHERE code_status_id = 3)
UNION ALL
SELECT * FROM t_release t where release_id IN 
    (SELECT MAX(release_id) FROM t_release x WHERE 
    NOT EXISTS (SELECT 1 FROM t_release y WHERE code_status_id = 3 and y.code_id = x.code_id))
   GROUP BY code_id)

第一个查询将获取所有付费的内容,另一个查询将获取尚未付款的最后一个release_id。

答案 1 :(得分:1)

您可以使用MySQL user defined variables

来实现此目的
SELECT 
t.release_id,
t.code_id,
t.code_status_id,
t.code_created_date
FROM 
(
  SELECT 
  *,
  IF(@sameCodeID = code_id, @rn := @rn + 1,
     IF(@sameCodeID := code_id, @rn := 1, @rn := 1)
   ) AS row_number
  FROM t_release t
  CROSS JOIN (SELECT @sameCodeID := 0, @rn := 1) AS var
  ORDER BY code_id, code_status_id DESC, release_id DESC
) AS t
WHERE t.row_number <= 1
ORDER BY t.code_id

SEE DEMO