在 sql 中计算每行中属性值的出现次数,区分大小写
Count occurence of attribute values in each row with case-sensitive in sql
我有table(notes_subject)结构-
+--------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+----------------+
| id | int | NO | PRI | NULL | auto_increment |
| user_id | int | NO | | NULL | |
| note_id | varchar(25) | NO | MUL | NULL | |
| subject_name | text | NO | | NULL | |
+--------------+-------------+------+-----+---------+----------------+
并且数据存储在这个table-
+----+---------+--------------+--------------+
| id | user_id | note_id | subject_name |
+----+---------+--------------+--------------+
| 10 | 2 | UdMs870BSswp | CN |
| 12 | 2 | 8stMvslwIGr2 | CN |
| 13 | 2 | PB3KNbbFkaUm | cn |
+----+---------+--------------+--------------+
注意:CN 和 cn(小写)不同。
我想计算 user_id
在这个 table 中每个 subject_name
的出现次数。所以我 运行 查询 -
SELECT subject_name, COUNT(subject_name)
FROM notes_subject where user_id=2 GROUP BY subject_name;
它获取了 -
+--------------+---------------------+
| subject_name | COUNT(subject_name) |
+--------------+---------------------+
| CN | 3 |
+--------------+---------------------+
但这不是正确的结果,因为 CN 和 cn 不同。
我还想要结果中的 id,user_id,note_id。
如果您的数据库支持 window functions
,我想您需要这样的东西。尝试使用 collate utf8mb4_bin
看看这是否有助于您需要的区分大小写
select id,
user_id,
note_id,
subject_name,
count(subject_name collate utf8mb4_bin) over (partition by user_id, subject_name collate utf8mb4_bin)
from notes_subject;
如果window functions
没有问题,也可以单独聚合,再加入到主table。
发布另一个替代方案,由 selecting the group by column as a binary field
.
在 dbfiddle 中使用 MYSQL 8.0 版本
进行了测试
SELECT cast(subject_name as binary) subject_name, COUNT(subject_name) cnt
FROM notes_subject
WHERE user_id=2
GROUP BY cast(subject_name as binary)
你可以简单地使用二进制来转换它。
SELECT count(*), CAST(subject_name as BINARY) AS lastname_cs
FROM notes_subject where user_id=2
GROUP BY CAST(subject_name as BINARY);
我有table(notes_subject)结构-
+--------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+----------------+
| id | int | NO | PRI | NULL | auto_increment |
| user_id | int | NO | | NULL | |
| note_id | varchar(25) | NO | MUL | NULL | |
| subject_name | text | NO | | NULL | |
+--------------+-------------+------+-----+---------+----------------+
并且数据存储在这个table-
+----+---------+--------------+--------------+
| id | user_id | note_id | subject_name |
+----+---------+--------------+--------------+
| 10 | 2 | UdMs870BSswp | CN |
| 12 | 2 | 8stMvslwIGr2 | CN |
| 13 | 2 | PB3KNbbFkaUm | cn |
+----+---------+--------------+--------------+
注意:CN 和 cn(小写)不同。
我想计算 user_id
在这个 table 中每个 subject_name
的出现次数。所以我 运行 查询 -
SELECT subject_name, COUNT(subject_name)
FROM notes_subject where user_id=2 GROUP BY subject_name;
它获取了 -
+--------------+---------------------+
| subject_name | COUNT(subject_name) |
+--------------+---------------------+
| CN | 3 |
+--------------+---------------------+
但这不是正确的结果,因为 CN 和 cn 不同。
我还想要结果中的 id,user_id,note_id。
如果您的数据库支持 window functions
,我想您需要这样的东西。尝试使用 collate utf8mb4_bin
看看这是否有助于您需要的区分大小写
select id,
user_id,
note_id,
subject_name,
count(subject_name collate utf8mb4_bin) over (partition by user_id, subject_name collate utf8mb4_bin)
from notes_subject;
如果window functions
没有问题,也可以单独聚合,再加入到主table。
发布另一个替代方案,由 selecting the group by column as a binary field
.
在 dbfiddle 中使用 MYSQL 8.0 版本
进行了测试SELECT cast(subject_name as binary) subject_name, COUNT(subject_name) cnt
FROM notes_subject
WHERE user_id=2
GROUP BY cast(subject_name as binary)
你可以简单地使用二进制来转换它。
SELECT count(*), CAST(subject_name as BINARY) AS lastname_cs
FROM notes_subject where user_id=2
GROUP BY CAST(subject_name as BINARY);