Select field not empty

1

demo table

 id  imagem       flash       codigo
 1   fda.jpg
 2             asdfasd.sfw
 3                           adsense

is there any way to SELECT only non-empty? if so what is the best way in PHP or SQL?

          $sql = "SELECT * FROM publicidade ORDER BY RAND() LIMIT 6";

         if flash,image = "" echo codgio

         if codigo,image = "" echo flash

Is it around or does it have some more appropriate method?

    
asked by anonymous 09.03.2015 / 21:57

1 answer

3

You can use the CASE expression in the select, for example: / p>

SELECT
  CASE
    WHEN imagem != '' THEN imagem
    WHEN flash != '' THEN flash
    WHEN codigo != '' THEN codigo
    ELSE NULL
  END AS val
FROM publicidade
ORDER BY RAND()
LIMIT 6

And in php, with the result of this query you can use:

echo $row['val'];
    
09.03.2015 / 22:20