Mysql结构和日期:
--
-- Table structure for table `site_links`
--
CREATE TABLE IF NOT EXISTS `site_links` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(15) NOT NULL,
`link_title` varchar(255) NOT NULL,
`link_url` text NOT NULL COMMENT,
`status` tinyint(1) NOT NULL DEFAULT '0',
`views_count` int(11) unsigned NOT NULL DEFAULT '0',
`unlocks_count` int(11) unsigned NOT NULL DEFAULT '0',
`report_count` int(11) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=74 ;
--
-- Dumping data for table `site_links`
--
INSERT INTO `site_links` (`id`, `username`, `link_title`, `link_url`, `status`, `views_count`, `unlocks_count`, `report_count`) VALUES
(56, 'john', 't1', 'http://google.com', 1, 4, 0, 0),
(57, 'john', 't2', 'http://google.com', 1, 0, 0, 0),
(58, 'james', 't3', 'http://google.com', 1, 3, 0, 0),
(59, 'dave', 't4', 'http://google.com', 1, 8, 0, 0),
(60, 'john', 't4', 'http://google.com', 1, 5, 0, 0),
我需要为用户名“john”加上“view_count”的值,所以输出应为“9”
我试过这个
<?php
$dpl = $db->query("SELECT SUM(`views_count`) FROM site_links WHERE username='john'",true);
echo $dpl;
?>
但它无法正常工作,我收到此错误“资源ID#92”
我怎么能这样做?
由于
答案 0 :(得分:2)
我假设您正在使用mysqli或其他东西
<?php
$dpl = $db->query("SELECT SUM(`views_count`) as `total` FROM site_links WHERE username='john'");
$row = $dpl->fetch_assoc();
echo $row['total'];
?>