我尝试计算出现次数并在之后添加分组依据,但出现错误
SELECT list is not in GROUP BY clause and contains nonaggregated column ‘bdd.my_table.text' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
我的 table :
id | book | chapter | text
1 | 1 | 1 | 'hello hello world'
2 | 1 | 2 | 'hello hello hello hello'
3 | 1 | 3 | 'world'
4 | 1 | 4 | 'hello test'
我执行我的请求:
SELECT
book,
chapter,
text,
ROUND (
(
LENGTH(text)
- LENGTH( REPLACE ( text, "hello", "") )
) / LENGTH("hello")
) AS count
FROM my_table
WHERE book=43
GROUP BY chapter
我想要这个结果:
book | chapter | count
----------------------
43 | 1 | 2
43 | 2 | 4
43 | 3 | 0
43 | 4 | 1
我尝试在删除“only_full_group_by”的请求之前添加此内容,但我遇到了同样的问题
SET SESSION group_concat_max_len = 1000000;
最佳答案
试试这个,虽然我到现在还不知道你到底想做什么,但是试试吧。
SELECT
book,
chapter,
group_concat(text) as text,
ROUND (
(
LENGTH(group_concat(text))
- LENGTH( REPLACE ( group_concat(text), "hello", "") )
) / LENGTH("hello")
) AS `count`
FROM my_table
GROUP BY chapter, book
关于mysql - 使用 SQL 计算字符串和 Group By 中字符的出现次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42685283/