mysql按照本身定義的值顯示若是爲空的就顯示0

題目:一張表:mysql

CREATE TABLE `tbl_time` (sql

  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,spa

  `time` char(10) NOT NULL DEFAULT '',io

  PRIMARY KEY (`id`)select

) ENGINE=InnoDB;nio

mysql> select * from tbl_time;im

+----+------------+ant

| id | time       |co

+----+------------+join

|  1 | 2015-10-27 | 

|  2 | 2015-10-27 | 

+----+------------+

可是我指望的結果是

2015-10-27,2015-10-28,2015-10-29,2015-10-30的2,0,0,0

並且只能用sql 語句

解決:

mysql> select a.tmp,if(time is null,0,count(*))as ant from (select '2015-10-27' as tmp union select '2015-10-28' union select '2015-10-29' union select '2015-10-30') a left join tbl_time on a.tmp=tbl_time.time group by a.tmp; 

+------------+-----+

| tmp        | ant |

+------------+-----+

| 2015-10-27 |   2 | 

| 2015-10-28 |   0 | 

| 2015-10-29 |   0 | 

| 2015-10-30 |   0 | 

+------------+-----+

相關文章
相關標籤/搜索