mysql - 我正在尝试在 sql 中运行查询,在其中将字段更改为小写以使其一致并对不同字段求和
问题描述
我有具有站点搜索的字段 evar4,文本在不同的情况下导致搜索相同单词的两个不同行,例如
evar4 searches
games 3236
Games 564
我想将所有 evar4 转换为小写字段并为所有相同的术语显示一个值,即,
e.g.
evar4 searches
games 3800
我已将字段转换为小写,但我不确定如何在 sql 查询中对这些字段进行求和,并且只显示同一单词的一行。
Select lcase(evar4), count (evar4) as searches from adobe_hits
where (date(date_time) between '2019-01-01' and '2019-01-31')
and post_prop46 like ('www:')
and exclude_hit like '0'
and hit_source not in (5,7,8,9)
group by evar4
order by searches desc;
sample result:
Original Result:
evar4 searches
games 3236
recently played games 1326
mahjongg solitaire 633
Games 564
Result after lcase:
lcase(evar4) searches
games 3236
recently played games 1326
mahjongg solitaire 633
games 564
desired result:
lcase(evar4) searches
games 3800
recently played games 1326
mahjongg solitaire 633
解决方案
您需要按字段的小写版本进行分组。
Select lcase(evar4), count (evar4) as searches from adobe_hits
where (date(date_time) between '2019-01-01' and '2019-01-31')
and post_prop46 like ('www:')
and exclude_hit like '0'
and hit_source not in (5,7,8,9)
group by lcase(evar4)
order by searches desc;
如果为表达式分配别名,则可以按该别名进行分组。
Select lcase(evar4) as l_evar4, count (evar4) as searches from adobe_hits
where (date(date_time) between '2019-01-01' and '2019-01-31')
and post_prop46 like ('www:')
and exclude_hit like '0'
and hit_source not in (5,7,8,9)
group by l_evar4
order by searches desc;
另一个避免首先这样做的解决方案是声明该列使用不区分大小写的排序规则。然后所有比较,包括分组,都将忽略大小写。
推荐阅读
- javascript - 编写一个可以与 then() 与 axios 输出链接的函数
- oauth - Google Oauth 登录不断重定向
- azure-active-directory - Microsoft Graph API 报告 B2B 用户已存在,但用户在 Azure 门户中不可见
- excel - 嵌套的 IF 语句,不能返回“0”,因为第二个 IF 语句在路上
- ruby-on-rails - Curl Call 从 Ruby on rails 不返回任何内容
- mysql - MySql 事务会阻塞其他事务吗?
- python - 使用 socket.socket 将数据连续发送到两个不同的 IP:NAT 保持相同端口是标准行为吗?
- postgresql - 计算同一车辆 postgresql 服务的不同位置的数量
- ios - PlacesAPI 的 OVER_QUERY_LIMIT 错误原因
- c - memset 的奇怪问题,为什么它会将我的变量归零?