1

I'm trying to translate this Sql Server statement into MySQL

IF(SELECT count(*) FROM MyTable) = 0 THEN
// Do Something
ELSE
// Do Another Thing

I just can't make it work.

I can't even make some variables do their job. MySQL help isn't helpfull at all.

Can anyone tell me the correct syntax?

Thanks

PEM1977
  • 11
  • 1
  • 1
    Can you explain what *Do Something* look like ? – M Khalid Junaid Jun 21 '18 at 05:50
  • What have you tried so far? Also can you answer Khalid's question? Thanks! – Max von Hippel Jun 21 '18 at 05:53
  • `IF((SELECT count(*) FROM MyTable) = 0, /* Do Something */, /* Do Another Thing */)` – codtex Jun 21 '18 at 06:14
  • If statements of this kind are only allowed in stored programs(procedures,function,triggers,events) in mysql is your code in any of these? – P.Salmon Jun 21 '18 at 06:26
  • see https://dev.mysql.com/doc/refman/5.7/en/if.html, confusingly (perhaps) there is also a control flow function https://dev.mysql.com/doc/refman/8.0/en/control-flow-functions.html and this is worth reading https://stackoverflow.com/questions/11754781/how-to-declare-a-variable-in-mysql – P.Salmon Jun 21 '18 at 06:32

1 Answers1

0
SELECT
    ID,
    IF(
        someVal = CURRENT_DATE(),
        'output when true',
        'output when false'
    ) AS someResult,
    col2
FROM foo
WHERE
    IF(
        (SELECT COUNT(*) FROM foo) = 0,
        ...,
        ...
    );

This should be enough for you to understand how to do it properly ;)

DataVader
  • 740
  • 1
  • 5
  • 19