仅获取 customer_ID 的一个值 where 多行 where

get only one value for customer_ID where multiple rows for where

大家好,这是我的查询 - 如果我 运行 这会得到多个客户 ID,但我只想 select 一个客户 ID

SELECT payments.customer_id,
       payments.reason,
       payments.date
FROM payments  
WHERE reason like '%error%' 
ORDER BY payments.date DESC

这是我尝试使用

组时收到的错误消息

Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'reason' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (errno 1055) (sqlstate 42000)

SELECT DISTINCT payments.customer_id, 
                payments.reason,
                payments.date FROM payments
WHERE reason like '%error%' 
ORDER BY payments.date DESC

DISTINCT 会为您解决这个问题

你需要以下 -

SELECT p.customer_id,
       p.reason,
       MAX(p.date) max_date
FROM payments p
WHERE p.reason like '%error%' 
GROUP BY p.customer_id,
         p.reason
ORDER BY max_date DESC