簡體   English   中英

MySQL如何獲取每個關鍵字的行數

[英]MySQL how to get count of rows for each keyword

我在table1中有一個名為text的字段。 我如何計算有多少行包含單詞“ test”,“ test2”和“ test3”?

到目前為止,我有:

SELECT count(*) FROM table WHERE text LIKE "%test%" OR text LIKE "%test2%" OR text LIKE "%test3%"

但這只會返回全部計數

我需要類似的東西:

test 100
test2 115
test3 12

誰能指出我正確的方向?

也許UNION會很適合您:

SELECT
    'test' AS `search_term`,
    COUNT(*) AS `term_count`
FROM table
WHERE text LIKE '%test%'
UNION
SELECT
    'test2' AS `search_term`,
    COUNT(*) AS `term_count`
FROM table
WHERE text LIKE '%test2%'
UNION
SELECT
    'test3' AS `search_term`,
    COUNT(*) AS `term_count`
FROM table
WHERE text LIKE '%test3%'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM