gpt4 book ai didi

mysql - 一个字符出现在字符串的第一个位置和第二个位置的频率是多少?

转载 作者:行者123 更新时间:2023-11-30 22:20:13 25 4
gpt4 key购买 nike

我正在尝试回答这个问题:

How often does a character occur in the first position versus the second position of a string?

在 Mysql 上使用 SQL 查询。

但是我得到一个语法错误。

代码:

SELECT 
onechar,
ASCII(onechar) as asciival,
COUNT(*) as cnt,
SUM(CASE WHEN pos = 1 THEN 1 ELSE 0 END) as pos_1,
SUM(CASE WHEN pos = 2 THEN 1 ELSE 0 END) as pos_2
FROM (
(SELECT
SUBSTRING(`city`, 1, 1) as onechar,
1 as pos
FROM `orders`
WHERE LEN(`city` >= 1 )

UNION ALL

(SELECT
SUBSTRING(`city`, 2, 1) as onechar,
2 as pos
FROM `orders`
WHERE LEN(`city` >= 2)
)
GROUP BY onechar
ORDER BY onechar

错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'GROUP BY onechar ORDER BY onechar LIMIT 0, 30' at line 1

尝试了几种方法都没有成功。

谁能帮我解释一下这个问题?

最佳答案

括号看起来不正确,查询缺少派生表的别名。此外,因为 mysql 将 bool 值计算为 1 或 0,您可以简化 sum 语句。试试这个:

SELECT onechar, ASCII(onechar) as asciival, COUNT(*) as cnt,
SUM(pos = 1) as pos_1,
SUM(pos = 2) as pos_2
FROM (
SELECT SUBSTRING(`city`, 1, 1) as onechar, 1 as pos
FROM `orders` WHERE LENGTH(`city`) >= 1
UNION ALL
SELECT SUBSTRING(`city`, 2, 1) as onechar, 2 as pos
FROM `orders` WHERE LENGTH(`city`) >= 2
) t
GROUP BY onechar
ORDER BY onechar

关于mysql - 一个字符出现在字符串的第一个位置和第二个位置的频率是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36831232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com