简体   繁体   中英

How to check Null values in mysql

I am using laravel framework to develop web application,i am using soft-deletes so while fetching the data from database i have to check weather the column is null or not,for that i wrote a following query

QueryBuilder

$today= "2022-09-23 00:00:00";
$this->repository->pushCriteria(new WhereCriteria('date_of_leaving', $today));

querylog

array (
    'query' => 'select * from `employees` where `date_of_leaving` = ? and `employees`.`deleted_at` is null',
    'bindings' => 
    array (
      0 => '2022-09-23 00:00:00',
    ),
    'time' => 2.36,
  ),

table structure

+-----------------+-----------------+------+-----+---------+----------------+
| Field           | Type            | Null | Key | Default | Extra          |
+-----------------+-----------------+------+-----+---------+----------------+
| id              | bigint unsigned | NO   | PRI | NULL    | auto_increment |
| name            | varchar(255)    | NO   |     | NULL    |                |
| email           | varchar(255)    | NO   |     | NULL    |                |
| joining_date    | timestamp       | NO   |     | NULL    |                |
| manager_id      | bigint unsigned | NO   | MUL | NULL    |                |
| image_path      | varchar(255)    | YES  |     | NULL    |                |
| date_of_leaving | timestamp       | YES  |     | NULL    |                |
| still_working   | timestamp       | YES  |     | NULL    |                |
| deleted_at      | timestamp       | YES  |     | NULL    |                |
+-----------------+-----------------+------+-----+---------+----------------+

data

+---------------------+---------------------+
| date_of_leaving     | deleted_at          |
+---------------------+---------------------+
| 2022-09-23 00:00:00 | 2022-09-23 11:47:11 |
| 2022-09-23 00:00:00 | 2022-09-23 12:36:46 |
| 2022-09-23 00:00:00 | 2022-09-23 13:09:55 |
| NULL                | NULL                |
| 2022-09-06 00:00:00 | NULL                |
| NULL                | NULL                |
| NULL                | NULL                |
| NULL                | NULL                |
| NULL                | 2022-09-23 11:45:01 |
| NULL                | NULL                |
| NULL                | NULL                |
+---------------------+---------------------+

Actually in database three matching records are there with the above condition but this query is not fetching the data,i was suspecting deteled_at was considering NULL as string

Try This.

array ( 'query' => 'select * from employees where date_of_leaving Is Null and employees . deleted_at is null', 'bindings' => array ( 0 => '2022-09-23 00:00:00', ), 'time' => 2.36, ),

Try doing this, i believe the package you are using does not support Laravel's softDelete() feature.

$this->repository->pushCriteria(new WhereCriteria(['date_of_leaving',=> $today,'deleted_at'=>Null]));

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