简体   繁体   中英

How to get a new column with total number of rows with value mysql

I have table that looks like this

      Date|Col1|Col2|Col3

01-01-2016|abc |    |    |

02-01-2016|xyz |188 |    |

03-01-2016|100 |abc |155 |

I need to write a query that will give me a new column with total number of columns with value for each day.

      Date|Col1|Col2|Col3|count|

01-01-2016|abc |    |    |  1  |

02-01-2016|xyz |188 |    |  2  |

03-01-2016|100 |abc | 155|  3  |

In my actual data I have about 30 columns that I need to count for.

SELECT *,(IF(col1 IN (NULL,'') , 0, 1)+ IF(col2 IN (NULL,''), 0, 1)+ IF(col3 IN (NULL,''), 0, 1)) as count 
from table_name
group by date

The sample table:
在此处输入图片说明

The output:
在此处输入图片说明

Hope this solves!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM