表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
。
我在这里遇到了其他问题,但没有人喜欢我这样做的方式。
我目前正在使用两个sqls和更多代码:
select count(id) from table where id = 1 and select count(id) from table where id = 2
如果两者都在那里,我将继续我的其余部分。我怎样才能在MySql中做这样的事情?
答案 0 :(得分:5)
您可以使用子选择。
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
答案 1 :(得分: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";