I have two tables, one with monster names and their respective scores:
CREATE TABLE `mobscores` (
`name` TINYTEXT NOT NULL ,
`score` TINYINT NOT NULL
);
and one with the actual kills on my server:
CREATE TABLE `mobkills` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` tinytext NOT NULL,
`mob` tinytext NOT NULL,
`item` smallint(6) NOT NULL DEFAULT '0',
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
);
Now i could do the brute-force way and read the first table into a hash (Using Perl here, but that shouldn’t matter for the question.), read in mobkills line for line after telling MySQL that i want the kills from one player starting at $whateverdate and then use the returning mob field on the hash to retrieve the score to add to the totals.
But i know that SQL is powerful so that i don’t need to do that myself.
So, the question is, how do i tell MySQL to exchange, or even count together, the mob name for the score from table mobscores?
Something like this (fantasy syntax incoming as i don’t know the real one):
SELECT score (AS score FROM mobscores WHERE name=mob)
FROM mobkills
WHERE name= 'McMiner' AND date >= SomeQueryDateToStartFrom;
I don’t know if MySQL could even add up score for me so that i don’t need to do that by foot in the Perl script, but that would be optional.
Thanks in advance. 🙂
EDIT:
Yay, thanks to rkosegis hint i got this. 😀
SELECT name, sum( (SELECT score FROM mobscores WHERE mobscores.name = mobkills.mob))
as score
FROM mobkills group by name order by score desc
Try:
Alternatively, you could use an inner join…