Pregunta

I have a table the primary key of which is a composite formed on 3 columns:

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

Primary Key is combination of (userID, itemID, itemType )

Assume: itemID 1 is a BALL, and itemType 1 is BLACK COLOR.
so in below example,

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    |

Now I want to count ALL users which selected a BLACK BALL, How can I count the primary key of this table which is composite

Thanks in advanced.

¿Fue útil?

Solución 2

To count ALL users which selected a BLACK BALL, try this query:

SELECT COUNT(userID)
FROM table1
WHERE itemID = 1 AND itemType = 1

I am not sure what you mean by "How can I count the primary key of this table"

Otros consejos

I solved for this by concating the composite key, and counting that:

SELECT COUNT( CONCAT(userID, '-', itemID, '-', itemType) ) FROM myTable
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top