搜尋

首頁  >  問答  >  主體

php - mysql 如何对同一张表的相似的查询结果进行合并(就是条件不同而已)?

譬如
select count(*) from A where type = 'dog'

select count(*) from A where type = 'cat'

这两个怎么合并呢?

这里我只是举个例子
每个类型的结果我都是需要的,另外条件查询会更复杂的,可能还是 addtime > And addtime < **

PHPzPHPz2896 天前235

全部回覆(3)我來回復

  • PHPz

    PHPz2017-04-10 17:46:17

    直接用 UNION ALL 合并查询内容可否

    SELECT count(*), 'dog' as type FROM A WHERE type='dog' 
    UNION ALL
    SELECT count(*), 'cat' as type FROM A WHERE type='cat' AND addtime > '2016-0-11-14'

    回覆
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-10 17:46:17

    type IN ('dog','cat')

    回覆
    0
  • 天蓬老师

    天蓬老师2017-04-10 17:46:17

    select count(*) from A where (type = 'dog' or type = 'cat')

    回覆
    0
  • 取消回覆