I would like to use an if statement in a MySQL query to SELECT all of the information in a database where… if it returns true, or return the COUNT of the column named aid.
Pseudocode:
IF (COUNT(`AID`) > 0) {
SELECT * FROM `a_votes` WHERE `aid` = '1'
} ELSE {
RETURN 'No Rows'
}
Please could you tell me how I could do this?
Edit—
I tried the following query, but I am receiving the error Operand should contain 1 column(s)
SELECT IF( (
SELECT COUNT( aid ) AS `count`
FROM `a_votes`
WHERE `aid` =1 ) =0, 'true', (
SELECT *
FROM `a_votes`
WHERE `aid` =1
)
) AS message
If no rows match, a simple
COUNT(*))will returns a single row with0for the count. Wrapping the usualCOUNT(*)query in an outer query that eliminates that condition will return no rows.Edited:
Thanks to
@bisiclop, this is a better solution: