以下是我想在MYSQL_QUERY
中说的内容$query = "SELECT (SELECT COUNT(type) FROM msg WHERE view = 0 AND nd = 'test' AND type = 0) as inbox,
(SELECT COUNT(type) FROM msg WHERE view = 0 AND nd = 'test' AND type = 1) as sent,
(SELECT COUNT(type) FROM msg WHERE view = 0 AND nd = 'test' AND type = 2) as draft,
(SELECT COUNT(type) FROM msg WHERE view = 0 AND nd = 'test' AND type = 3) as spam,
(SELECT COUNT(type) FROM msg WHERE view = 0 AND nd = 'test' AND type = 4) as trash";
我喜欢使用PDO,我试过这个,但是我收到了一个错误:
$query=$db->prepare("SELECT
(SELECT COUNT(*) as inbox FROM msg WHERE view = :view AND nd = :nd AND type = :type),
(SELECT COUNT(*) as sent FROM msg WHERE view = :view AND nd = :nd AND type = :type),
(SELECT COUNT(*) as draft FROM msg WHERE view = :view AND nd = :nd AND type = :type),
(SELECT COUNT(*) as trash FROM msg WHERE view = :view AND nd = :nd AND type = :type)
");
$query->execute(array(
':view' => 0,
':nd' => 'test',
':type' => 1
));
$pageRow=$query->fetch(PDO::FETCH_NUM);
$inbox = $pageRow[0];
$sent = $pageRow[1];
$draft = $pageRow[2];
$trash = $pageRow[3];
答案 0 :(得分:1)
请尝试此查询:
SELECT IFNULL(SUM(type = 0), 0) as inbox,
IFNULL(SUM(type = 1), 0) as sent,
IFNULL(SUM(type = 2), 0) as draft,
IFNULL(SUM(type = 3), 0) as spam,
IFNULL(SUM(type = 4), 0) as trash
FROM msg
WHERE view = :view AND nd = :nd