Badmiral
Badmiral

Reputation: 1589

mysql if statement failing, not sure how to fix

Currently I have this statement

IF(0.5 < SELECT (FLOOR(10*RAND()*pResourcium) AS TestVar FROM siegeverse.rooms rm) AS T))
THEN SELECT 'TEST'

Any idea why it is failing? Trying to get it to output test for each row if the selected element is greater than 0.5

Thanks!

Upvotes: 2

Views: 59

Answers (1)

Vikdor
Vikdor

Reputation: 24124

SELECT statement in such scalar comparison contexts should return only one row. In your case, it might be returning multiple rows and so is failing. The following rewritten query is close to what you are interested in:

IF EXISTS (SELECT * 
FROM
    (SELECT
         CASE 
             WHEN (FLOOR(10*RAND()*pResourcium) >= 0.5 THEN 'TEST'
             ELSE NULL
         END AS TestVar
     FROM 
         siegeverse.rooms rm
    ) AS A
WHERE A.TestVar IS NOT NULL)
THEN
     <run the stored procedure>

Upvotes: 4

Related Questions