簡體   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