I program a site and I can't get this query right.
I have a table that contain the channels
select * from `channels`
and I have another table that contain the statistics of viewing every page in the site.
To get the all visits to a specific channel I will write
SELECT SUM(ip) AS visits FROM `log` where `module` = 'channels' and mid = '15'
where 15 is equal the id of a row in the channels table.
The query that I need to write should use a join with sum to select * from channels and add an extra cell (value) to get the number of visits for every id in Channels.
From comments to answers:
The table called Channels contains columns id and name. The table called Log contains columns ip, module, mid. The
log.module = 'channel';log.mid = channel.id. Every channel id (such as '15') can get all of its visits by the querySELECT SUM(ip) AS visits FROM log where module = 'channels' and mid = '15'.
Roughly:
CREATE TABLE Channels (ID INTEGER, Name CHAR(30), PRIMARY KEY ID);
CREATE TABLE Log (IP CHAR(16), Module CHAR(10), MID INTEGER REFERENCES Channels(ID));