catfan / Medoo

The lightweight PHP database framework to accelerate the development.
https://medoo.in
MIT License
4.84k stars 1.15k forks source link

SELECT (...) WHERE value = '' OR value IS NULL #553

Closed Krzysiu closed 7 years ago

Krzysiu commented 7 years ago

How to select rows with WHERE with both NULL or empty string? I tried: $db->select('foo', 'id', XXX); where XXX was:

Of course I can make raw query, but I'd love to use native way to get something, that would result WHERE value = '' OR value IS null. When I do ['value' => null] it works well. It just fails when there's NULL or something other.

catfan commented 7 years ago

In this special case, you can do this with commented key.

$database->select('foo', 'id', [
    'OR' => [
        'AND #Null' => [
            'description' => null
        ],
        'AND #Empty' => [
            'description' => ''
        ]
    ]
]);

// Or simplified:
$database->select('foo', 'id', [
    'OR' => [
        'description' => null,
        'AND #Empty' => [
            'description' => ''
        ]
    ]
]);
Krzysiu commented 7 years ago

Thanks, that does the trick!

grogorick commented 1 year ago

// Or further simplified:

$database->select('foo', 'id', [
    'OR' => [
        'description' => null,
        'description #empty' => ''
    ]
]);