如何过滤MySQL结果以显示每个用户的LIMIT 1?

时间:2014-03-23 15:57:57

标签: mysql sql filter greatest-n-per-group

以下是SQL命令结果的屏幕截图:

enter image description here

这是SQL命令:

SELECT inl_cbsubs_subscriptions.user_id AS cbsubsUserID, inl_cbsubs_subscriptions.id AS cbsubsId, inl_cbsubs_subscriptions.status AS status, inl_cbsubs_payment_items.subscription_id     AS paymentSubID, inl_cbsubs_payment_items.stop_date AS paymentStopDate, inl_cbsubs_payment_items.id AS paymentID
FROM inl_cbsubs_subscriptions
INNER JOIN inl_cbsubs_payment_items
ON inl_cbsubs_subscriptions.id=inl_cbsubs_payment_items.subscription_id
WHERE status='C'
ORDER BY paymentID DESC;

我希望调整此命令,以便我只显示基于每个用户的最新结果。换句话说,在这种情况下,这就是表格应该类似的东西:

enter image description here

正如您所看到的,cbsubsUserID每个ID只显示一个结果,而596 ID的结果有多个。

4 个答案:

答案 0 :(得分:0)

我认为您可以使用GROUP BY来实现此目标

SELECT inl_cbsubs_subscriptions.user_id AS cbsubsUserID, inl_cbsubs_subscriptions.id AS cbsubsId, inl_cbsubs_subscriptions.status AS status, inl_cbsubs_payment_items.subscription_id     AS paymentSubID, inl_cbsubs_payment_items.stop_date AS paymentStopDate, inl_cbsubs_payment_items.id AS paymentID
FROM inl_cbsubs_subscriptions
INNER JOIN inl_cbsubs_payment_items
ON inl_cbsubs_subscriptions.id=inl_cbsubs_payment_items.subscription_id
WHERE status='C'
GROUP BY inl_cbsubs_subscriptions.user_id
ORDER BY paymentID DESC;

答案 1 :(得分:0)

SELECT inl_cbsubs_subscriptions.user_id AS cbsubsUserID, MAX(inl_cbsubs_subscriptions.id) AS cbsubsId, inl_cbsubs_subscriptions.status AS status, MAX(inl_cbsubs_payment_items.subscription_id)     AS paymentSubID, x.stop_date AS paymentStopDate, x.blah AS paymentID
FROM inl_cbsubs_subscriptions
INNER JOIN inl_cbsubs_payment_items
ON inl_cbsubs_subscriptions.id=inl_cbsubs_payment_items.subscription_id
INNER JOIN
(SELECT inl_cbsubs_payment_items.stop_date as stop_date ,
 MAX(inl_cbsubs_payment_items.id) as blah
 FROM inl_cbsubs_subscriptions
 INNER JOIN inl_cbsubs_payment_items
 ON inl_cbsubs_subscriptions.id=inl_cbsubs_payment_items.subscription_id
 WHERE status='C'
 GROUP BY inl_cbsubs_subscriptions.user_id)as x
ON x.stop_date=inl_cbsubs_payment_items.stop_date AND x.blah=inl_cbsubs_payment_items.id
WHERE status='C'
GROUP BY inl_cbsubs_subscriptions.user_id
ORDER BY paymentID DESC;

答案 2 :(得分:0)

如果您想获得最新结果,最好的方法是使用not exists

SELECT s.user_id AS cbsubsUserID, s.id AS cbsubsId, s.status AS status,
       i.subscription_id AS paymentSubID, i.stop_date AS paymentStopDate, i.id AS paymentID
FROM inl_cbsubs_subscriptions s INNER JOIN
     inl_cbsubs_payment_items i
     ON s.id = i.subscription_id
WHERE s.status = 'C' and
      not exists (select 1
                  from inl_cbsubs_payment_items i2
                  where i2.subscription_id = i.subscription_id and
                    i2.id > i.id
                 )
ORDER BY paymentID DESC;

您需要inl_cbsubs_payment_items(subscription_id, paymentid)上的索引。

这说的是:"获取给定订阅ID的所有项目,该订阅ID没有更大的订阅停止日期"。这是一种花哨的说法,让每个订阅ID和#34;让我最新,但它往往在数据库中效果最好。

答案 3 :(得分:0)

要获得每组的最新记录,您可以这样做

SELECT DISTINCT
s.user_id AS cbsubsUserID, 
s.id AS cbsubsId, 
s.status AS `status`, 
si.subscription_id     AS paymentSubID,
si.stop_date AS paymentStopDate, 
si.id AS paymentID
FROM inl_cbsubs_subscriptions s
INNER JOIN inl_cbsubs_payment_items si
ON s.id=si.subscription_id
 INNER JOIN (SELECT MAX(stop_date) stop_date ,subscription_id ,MAX(id) id
FROM inl_cbsubs_payment_items 
GROUP BY subscription_id ) max_si
ON 
(max_si.subscription_id=si.subscription_id 
AND max_si.id =si.id 
AND  max_si.stop_date = si.stop_date )
WHERE `status`='C'
ORDER BY paymentID DESC;