Skip to content

Instantly share code, notes, and snippets.

@kakobotasso
Created January 9, 2013 11:29
Show Gist options
  • Save kakobotasso/4492453 to your computer and use it in GitHub Desktop.
Save kakobotasso/4492453 to your computer and use it in GitHub Desktop.
Case When - MySQL
SELECT a.idanuncio,
a.titulo,
a.estado,
(CASE WHEN a.preco_machos = 0 AND a.preco_femeas != 0 THEN a.preco_femeas WHEN a.preco_femeas = 0 && a.preco_machos != 0 THEN a.preco_machos WHEN a.preco_machos <= a.preco_femeas THEN a.preco_machos ELSE a.preco_femeas END) as preco,
ga.titulo AS imagem,
a.identificacao
FROM anuncio AS a
INNER JOIN galeria_anuncio AS ga ON a.idanuncio = ga.idanuncio
WHERE ga.posicao = 0
GROUP BY a.idanuncio
ORDER BY a.idanuncio DESC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment