我想从数据库中获取观看次数最多的照片,但我不知道如何实现这一点。这是我目前的SQL:
SELECT * FROM photos AS p, viewers AS v
WHERE p.id = v.id_photo
GROUP BY v.id_photo
数据库:
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`)
)
中的数据viewers
如下所示:
(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');
来自数据库的一行,用于photos
了解这些行在此数据库中的外观:
(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')
目前 SQL 只打印 ID 为 84 的照片。在这种情况下,它是错误的 - 它应该打印出 ID 为 85 的照片。
我该如何解决这个问题?
提前致谢。