0

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”

请问我该怎么做?

谢谢

4

1 回答 1

3

我假设你正在使用 mysqli 或其他东西

<?php

$dpl = $db->query("SELECT SUM(`views_count`) as `total` FROM site_links WHERE username='john'");

$row = $dpl->fetch_assoc();

echo $row['total'];

?>
于 2013-02-27T23:45:28.820 回答