我有一张桌子叫reservations
. 我想计算所有firstName
字段guestFirstName
。
我试过了:
$records = $dbh->query('SELECT count(firstName, guestFirstName) from reservations')->fetchColumn;
这给了我一个错误。如何计算所有firstName, guestFirstName
记录?
尝试以下操作:
$query = 'SELECT count(firstName) + count(guestFirstName) as total ' .
'FROM reservations';
$result = $dbh->query($query)->fetch();
echo $result['total']
SQL Fiddle 验证这一点:http ://www.sqlfiddle.com/#!2/42bad/2
SQL 的COUNT()
函数不计算NULL
值。
感谢@ChaotiCc 的帮助
您需要对count()
每一列,更改如下
'SELECT count(firstName) as total_firstname, count(guestFirstName) as guest_firstname from reservations'