MySQL displays 0 if it is empty, as defined by its value.

Source: Internet
Author: User

Title: A table:

CREATE TABLE ' Tbl_time ' (

' id ' int (ten) unsigned not NULL auto_increment,

' Time ' char (TEN) not NULL DEFAULT ' ',

PRIMARY KEY (' id ')

) Engine=innodb;

Mysql> select * from Tbl_time;

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

| ID | Time |

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

| 1 | 2015-10-27 |

| 2 | 2015-10-27 |

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

But the result I'm hoping for is

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

and only with SQL statements

Solve:

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 |

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




MySQL displays 0 if it is empty, as defined by its value.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.