Luís Morim
Luís Morim

Reputation: 9

Select values from not empty column in mysql

Table Photos-------

photo1  imagem1.jpg
photo2  imagem2.jpg
photo3  empty
photo4  empty
photo5  enpty

I have a table, with 5 columns who represents photos. So, I want to select * from table, all columns... but if one or more of them are empty, I don't want to select. is that possible?

For example, if I do this

Select * from table Photos where photo1 is not null, and photo2 is not null, and photo3 is not null and photo4 is not null and photo5 is not null 

that is not possible, because if one of them are empty... the select don't give any values.

anyone know another way?

and also, i need that because if I want to do this: the problem is.... when I use php for example this

if($project['photo5']!='' and $project['photo5']!=null){ 
(here i show the picture)
}

If the row is empty... php is ignored and show a error image

Upvotes: 0

Views: 971

Answers (1)

xQbert
xQbert

Reputation: 35323

Concat() will return null if any one of the values concat is null (mySQL DOCS) Since you want to only show records where none are null concat together and check for not null.

Select * 
from table Photos 
where concat(photo1,photo2,photo3,photo4,photo5) is not null 

AND... your approach should work if you used or's instead of AND and possibly a not. I just find the above easier to read.

Upvotes: 1

Related Questions