mysql - mysql中如何计算复合主键

标签 mysql sql composite-key

我有一个表,其主键是由 3 列组成的组合:

userID      FK of users table  
itemID      FK of items table
itemType    FK of itemtypes table

主键是 (userID, itemID, itemType) 的组合

假设:itemID 1 是一个 BALL,itemType 1 是 BLACK COLOR。
所以在下面的例子中,

User 1 is selecting a BALL which is BLACK
User 2 is selecting a BALL which is WHITE
User 3 is selecting a BALL which is BLACK


+------+------+--------+
|userID|itemID|itemType| 
+------+------+--------+
|   1  |  1   |   1    |
+------+------+--------+
|   2  |  1   |   2    |
+------+------+--------+
|   3  |  1   |   1    |

现在我想统计所有选择了黑球的用户,我怎样才能统计这个复合表的主键

提前致谢。

最佳答案

我通过连接复合键解决了这个问题,并计算:

SELECT COUNT( CONCAT(userID, '-', itemID, '-', itemType) ) FROM myTable

关于mysql - mysql中如何计算复合主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20425167/

相关文章:

mysql - 在 amazon RDS 上的 mysql/mariadb 上创建用户时出现问题

mysql - 如何更新mysql中所有重复记录的列

php - 将 PHP 数组从 MySQL 编码为 JSON

sql - 电话数据库主要关键问题

jpa - 插入具有复合主键的对象

asp.net - 我是否应该对映射表使用复合键,该复合键也用于外键?

mysql - 使用 Entity Framework 代码优先方法建立一对一关系?

python - 如何显式引用字符串值(Python DB API/Psycopg2)

python - web2py 类型错误 : id() takes exactly one argument (0 given)

带FIRSTROW参数的SQL批量插入跳过以下行