Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to create cleaner sql syntax

Here is what I am trying to do:

select * from table
where in ('completedDate', 'completedBy', 'cancelDate', 'cancelBy') is not null

If the four columns above are not null I need to display the records. I know I would do this with several where/and clauses but trying to learn and make my new stuff cleaner.

Is what I am trying to do above possible in a cleaner way?

like image 772
James Wilson Avatar asked Jan 29 '26 12:01

James Wilson


2 Answers

If I understand correctly I guess you want to do that:

select * 
from table
where completedDate is not null
  and completedBy is not null
  and cancelDate is not null 
  and cancelBy is not null

Regarding clarity of code I don't see a better way to write it, that's what I would code anyway.

EDIT: I wouldn't really do that in this case, but if this is a very common condition you can add a computed column in the table (stored or not), or create a view on top of table, and do:

select * from view where importantFieldsAreNotNull = 1
like image 192
jods Avatar answered Jan 31 '26 01:01

jods


If I understand correctly, you want to return records where all four columns are not null?

The standard and (in my opinion) most readable way to do this would be:

Select
    *
From
    YourTable
Where
    Column1 IS NOT NULL AND
    Column2 IS NOT NULL AND
    Column3 IS NOT NULL AND
    Column4 IS NOT NULL;
like image 27
Josh Jay Avatar answered Jan 31 '26 03:01

Josh Jay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!