MySQL - Counting two things with different conditions

前端 未结 4 1609
忘掉有多难
忘掉有多难 2020-12-10 11:01

I want to count two things under different conditions in one query.

SELECT COUNT(*) AS count FROM table_name WHERE name = ?

and

<         


        
4条回答
  •  粉色の甜心
    2020-12-10 11:45

    What about simply:

    SELECT 
        SUM(IF(name = ?, 1, 0)) AS name_count,
        SUM(IF(address = ? AND port = ?, 1, 0)) AS addr_count
    FROM 
        table_name
    

提交回复
热议问题