I want to get the most viewed photo from the database but I don't know how I shall accomplish this. Here's my SQL at the moment:
SELECT * FROM photos AS p, viewers AS v
WHERE p.id = v.id_photo
GROUP BY v.id_photo
The databases:
CREATE TABLE IF NOT EXISTS `photos` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`photo_filename` varchar(50) NOT NULL,
`photo_camera` varchar(150) NOT NULL,
`photo_taken` datetime NOT NULL,
`photo_resolution` varchar(10) NOT NULL,
`photo_exposure` varchar(10) NOT NULL,
`photo_iso` varchar(3) NOT NULL,
`photo_fnumber` varchar(10) NOT NULL,
`photo_focallength` varchar(10) NOT NULL,
`post_coordinates` text NOT NULL,
`post_description` text NOT NULL,
`post_uploaded` datetime NOT NULL,
`post_edited` datetime NOT NULL,
`checkbox_approxcoor` enum('0','1') NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
)
CREATE TABLE IF NOT EXISTS `viewers` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`id_photo` int(10) DEFAULT '0',
`ipaddress` text NOT NULL,
`date_viewed` datetime NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
)
The data in viewers looks like this:
(1, 85, '3892a0ab97d6ff325f285b27b847070f', '2012-06-21 22:49:25'),
(2, 84, '3892a0ab97d6ff325f285b27b847070f', '2012-06-21 22:49:25'),
(3, 85, '3892a0ab97d6ff325f285b27b847070f', '2012-06-21 22:49:25');
One single row from the database for photos to understand how the rows looks like in this database:
(85, 'P1170986.JPG', 'Panasonic DMC-LX3', '2012-06-19 18:00:40', '3968x2232', '10/8000', '80', '50/10', '51/10', '', '', '2012-06-19 18:45:17', '0000-00-00 00:00:00', '0')
At the moment the SQL only prints the photo with ID 84. In this case it's wrong - it should print out the photo with ID 85.
How can I fix this problem?
Thanks in advance.