我尝试计算出现次数并在之后添加组,但我有错误
SELECT列表不在GROUP BY子句中,并且包含nonaggregated 专栏'bdd.my_table.text'这在功能上并不依赖于 GROUP BY子句中的列;这是不相容的 的sql_mode = only_full_group_by
我的表:
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;
答案 0 :(得分:1)
试试这个,即使我现在还不知道你到底想做什么,但试试吧。
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
答案 1 :(得分:0)
SELECT
列表中的所有列都应出现在GROUP BY
子句中。因此,请在SELECT
子句(或)中添加所有GROUP BY
列表列,从SELECT
列表中删除不在GROUP BY
子句中
SELECT
book,
chapter,
ROUND (
(
LENGTH(text)
- LENGTH( REPLACE ( text, "hello", "") )
) / LENGTH("hello")
) AS count
FROM my_table
WHERE book=43
GROUP BY book, chapter, text
答案 2 :(得分:0)
如果我理解正确,你可能想要这样的东西:
SELECT book, chapter,
SUM(LENGTH(REPLACE(text, 'hello', 'hellox')) - LENGTH(text)) as cnt
FROM my_table
WHERE book = 43
GROUP BY book, chapter;
请注意COUNT()
更简单 - 没有划分。这会将hello
替换为长一个字符的字符串,然后减去原始文本的长度。