Select records in order

2

I'm doing a virtual store from scratch and the customer asked me to do the following logic:

  • Selects first available products with photo
  • Then select those available without a photo
  • Next select unavailable with photo
  • then select the non-photo unavailable

The problem is that I have no idea how to do this, because I know only to perform the selection with the where status = '1' AND photo! = '' , how can I do to follow the above order?

    
asked by anonymous 01.08.2015 / 22:24

1 answer

1

It is possible to do only using ORDER BY of MySQL

SELECT * FROM 'tb_produto'
ORDER BY 'status' DESC, LENGTH('foto') > 0 DESC;

When using the LENGTH() function, the '' (empty) and NULL for photo values will be displayed last.

    
03.08.2015 / 21:03