我有一个表,其主键是由 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/