繁体   English   中英

仅在mysql中的特定列上变得与众不同

[英]Getting distinct only for a certain column in mysql

我正在尝试选择具有“响应”的不同列的行,但是这样做很麻烦。 请看一下我的代码:

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

如果删除查询中的“输入”列,则可以使用,但我也想选择“输入”列。 如果我在响应旁边放一个distinct关键字,它将显示一个错误。 我该怎么做才能获得列“输入”和不同的列响应?

您想使用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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM