Another quick SQL question
I have the following table structure, A -< B represents one A can have many B
User -< Computer -< ComputerConfiguration -< Benchmark >- GameVersion >- Game
They are开发者_如何学运维 connected with PK int keys.
One user can have many computers, computer can have many configurations, configuration can have many benchmarks, and benchmark has FK to game version which has FK to game
How can I count a total number of unique games for each user?
Can I count number of computers, benchmarks and games per user - all in one query?
To be 100% specific on the answer we'd need to know which DBE you're using.
Please note this is just pseudo code, the general idea though would be...
SELECT COUNT(DISTINCT Game)
FROM User
INNER JOIN Computer
INNER JOIN ComputerConfig...
to answer your next question, if you want all of those counts in a single query you'll need to employ some sub-queries.
SELECT User, r1.ComputerCount, r2.BenchmarkCount
FROM User
INNER JOIN (SELECT User as UserId, count(*) FROM User INNER JOIN Computer ON (...) GROUP BY User) as r1 ON (User.id = r1.UserId)
INNER JOIN (SELECT User as UserId, count(Distinct Benchmark) FROM ...) as r2 ON (User.id = r2.UserId)
... and so on
精彩评论