3

表格1

"id"    "name"      "description"   "path"                              "country"   "status"
"1"     "Title 1"   "Description 1" "US > Consumer > Home Applicances"  "US"        "0"
"2"     "Title 2"   "Description 2" "US > Business > Legal Charges"     "UK"        "0"

我正在尝试做两个与同一张桌子counts不同的事情。wheres

我在这里遇到了其他问题,但没有一个像我这样做的方式。

我目前正在使用两个 sql 和更多代码:

select count(id) from table where id = 1 and select count(id) from table where id = 2

如果两者都在那里,我将继续我的脚本的其余部分。我怎样才能在 MySql 中做这样的事情?

4

2 回答 2

6

您可以使用子选择。

SELECT * FROM 
  (SELECT COUNT(id) AS count_one FROM table WHERE id = 1) a,
  (SELECT COUNT(id) AS count_two FROM table WHERE id = 2) b
于 2013-06-09T13:31:44.763 回答
0

这可能会帮助你 -

    $query = "SELECT SUM(Case when id=1 then 1 else 0 end) as count_one,
SUM(Case when id=2 then 1 else 0 end) as count_two FROM table"; 
于 2017-12-11T10:49:41.327 回答