我有一个表,其主键是在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 |
现在我想要计算选择BLACK BALL的所有用户,如何计算此表的主键是复合
先谢谢。
答案 0 :(得分:3)
我通过合并复合键并计算:
来解决这个问题SELECT COUNT( CONCAT(userID, '-', itemID, '-', itemType) ) FROM myTable
答案 1 :(得分:2)
要计算选择BLACK BALL的所有用户,请尝试以下查询:
SELECT COUNT(userID)
FROM table1
WHERE itemID = 1 AND itemType = 1
我不确定你的意思是“如何计算此表的主键”