如何将此查询从 MSSQL 转换为 MySQL?
Posted
技术标签:
【中文标题】如何将此查询从 MSSQL 转换为 MySQL?【英文标题】:How to convert this query to MySQL from MSSQL? 【发布时间】:2022-01-15 06:26:01 【问题描述】:查询:
SELECT count(id) AS counter,
SUBSTRING(user_name, CHARINDEX('@', user_name) + 1, LEN(user_name)) AS domain,
comp_id
FROM user_stat
WHERE ok='y'
GROUP BY SUBSTRING(user_name, CHARINDEX('@', user_name) + 1, LEN(user_name)),
comp_id
ORDER BY counter DESC
这部分主要是我关心的:
SUBSTRING(user_name, CHARINDEX('@', user_name) + 1, LEN(user_name)) AS domain
我找不到解决方案。 .. 提前致谢!
【问题讨论】:
mysql 确实有一个用户手册,它包含大量有用的信息,就像 MySQL 提供的所有字符串处理函数一样。 Why not read it 【参考方案1】:您只需将LEN
更改为LENGTH
并将CHARINDEX
更改为LOCATE
:
https://dev.mysql.com/doc/refman/8.0/en/functions.html
SELECT count(id) AS counter,
SUBSTRING(user_name, LOCATE('@', user_name) + 1, LENGTH(user_name)) AS domain,
comp_id
FROM user_stat
WHERE ok='y'
GROUP BY SUBSTRING(user_name, LOCATE('@', user_name) + 1, LENGTH(user_name)),
comp_id
ORDER BY counter DESC
【讨论】:
感谢您的解释!看来它确实奏效了!【参考方案2】:SUBSTRING(user_name, LOCATE('@', user_name) + 1) AS domain
【讨论】:
以上是关于如何将此查询从 MSSQL 转换为 MySQL?的主要内容,如果未能解决你的问题,请参考以下文章
如何将此 MySQL SELECT 查询转换为 DELETE 查询?