简体   繁体   中英

MySql counting all those tupples where in Date Rows containing (NULL) and or '0000-00-00' values

i want to count all those rows from mysql table where in the "where clause" three columns having two possible values in it. ie ( NULL ) and OR 0000-00-00 . For this i have written the following sql which returns a result where i am not sure it is a perfect sql query or not. Please guide me if i am doing wrong.

      SELECT 
          COUNT(sno) AS totalCases 
      FROM
         myTable
       WHERE (dateOfTransferInstituion IS NULL OR dateOfTransferInstituion = '0000-00-00')
         AND 
             (date_remanded IS NULL OR date_remanded = '0000-00-00')
         AND 
             (date_restored IS NULL OR date_restored = '0000-00-00')

Thanks in advance.

I have read your task and I think your query is correct, but it is possible to do it more elegantly, like this :

  SELECT 
      COUNT(sno) AS totalCases 
  FROM
     myTable
   WHERE IFNULL(dateOfTransferInstituion,'0000-00-00') = '0000-00-00'
     AND 
         IFNULL(date_remanded,'0000-00-00') = '0000-00-00'
     AND 
         IFNULL(date_restored,'0000-00-00') = '0000-00-00'

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