仅对 mysql 中的特定列进行区分

Getting distinct only for a certain column in mysql

我正在尝试 select 具有不同列 'response' 的行,但我在执行此操作时遇到了问题。请看看我的代码:

select distinct input,response,
((response REGEXP '[[:<:]]a[[:>:]]')+(response REGEXP '[[:<:]]have[[:>:]]')+(response REGEXP '[[:<:]]I[[:>:]]')) as wordsFound, 
(LENGTH(response) - LENGTH(REPLACE(response, ' ', ''))+1) AS wordsCount 
FROM allData 
HAVING wordsFound > 0 
order by wordsFound desc, wordsCount asc, rand() LIMIT 30

如果我在查询中删除列 'input',它会起作用,但我还想删除 select 列 'input'。如果我将 distinct 关键字放在 response 旁边,它会显示错误。我应该怎么做才能同时获得列 'input' 和不同的列响应?

您想要使用 GROUP BY 子句而不是 DISTINCT

select input,response,
((response REGEXP '[[:<:]]a[[:>:]]')+(response REGEXP '[[:<:]]have[[:>:]]')+(response REGEXP '[[:<:]]I[[:>:]]')) as wordsFound, 
(LENGTH(response) - LENGTH(REPLACE(response, ' ', ''))+1) AS wordsCount 
FROM allData
GROUP BY response 
HAVING wordsFound > 0 
order by wordsFound desc, wordsCount asc, rand() LIMIT 30