Skip to main content
added 164 characters in body; edited tags
Source Link
ypercubeᵀᴹ
  • 98.6k
  • 13
  • 215
  • 305

Why does this query DELETE FROM test WHERE id=(SELECT id FROM (SELECT * FROM test) temp ORDER BY RAND() LIMIT 1);

DELETE FROM test 
WHERE id = ( SELECT id 
             FROM (SELECT * FROM test) temp 
             ORDER BY RAND() 
             LIMIT 1
           );

sometimes delete 1 row, sometimes 2 rows and sometimes nothing?

If I write it in this form: SET @var = (SELECT id FROM (SELECT * FROM test) temp ORDER BY RAND() LIMIT 1); DELETE FROM test WHERE id=@var;,

SET @var = ( SELECT id 
             FROM (SELECT * FROM test) temp 
             ORDER BY RAND() 
             LIMIT 1
           ); 
DELETE FROM test 
WHERE id=@var;

then it work correctly - is problem in subquery?

Why does this query DELETE FROM test WHERE id=(SELECT id FROM (SELECT * FROM test) temp ORDER BY RAND() LIMIT 1); sometimes delete 1 row, sometimes 2 rows and sometimes nothing?

If I write it in this form: SET @var = (SELECT id FROM (SELECT * FROM test) temp ORDER BY RAND() LIMIT 1); DELETE FROM test WHERE id=@var;, then it work correctly - is problem in subquery?

Why does this query

DELETE FROM test 
WHERE id = ( SELECT id 
             FROM (SELECT * FROM test) temp 
             ORDER BY RAND() 
             LIMIT 1
           );

sometimes delete 1 row, sometimes 2 rows and sometimes nothing?

If I write it in this form:

SET @var = ( SELECT id 
             FROM (SELECT * FROM test) temp 
             ORDER BY RAND() 
             LIMIT 1
           ); 
DELETE FROM test 
WHERE id=@var;

then it work correctly - is problem in subquery?

added 5 characters in body
Source Link
RolandoMySQLDBA
  • 184.3k
  • 33
  • 323
  • 531

Why does this query DELETE FROM test WHERE id=(SELECT id FROM (SELECT * FROM test) temp ORDER BY RAND() LIMIT 1); sometimes delete 1 row, sometimes 2 rows and sometimes nothing?

If I write it in this form: SET @var = (SELECT id FROM (SELECT * FROM test) temp ORDER BY RAND() LIMIT 1); DELETE FROM test WHERE id=@var;, then it work correctly - is problem in subquery?

Why this query DELETE FROM test WHERE id=(SELECT id FROM (SELECT * FROM test) temp ORDER BY RAND() LIMIT 1); sometimes delete 1 row, sometimes 2 rows and sometimes nothing?

If I write it in this form: SET @var = (SELECT id FROM (SELECT * FROM test) temp ORDER BY RAND() LIMIT 1); DELETE FROM test WHERE id=@var;, then it work correctly - is problem in subquery?

Why does this query DELETE FROM test WHERE id=(SELECT id FROM (SELECT * FROM test) temp ORDER BY RAND() LIMIT 1); sometimes delete 1 row, sometimes 2 rows and sometimes nothing?

If I write it in this form: SET @var = (SELECT id FROM (SELECT * FROM test) temp ORDER BY RAND() LIMIT 1); DELETE FROM test WHERE id=@var;, then it work correctly - is problem in subquery?

Tweeted twitter.com/#!/StackDBAs/status/40008706859077632
Source Link
tomas.lang
  • 253
  • 2
  • 5

Problem with MySQL subquery

Why this query DELETE FROM test WHERE id=(SELECT id FROM (SELECT * FROM test) temp ORDER BY RAND() LIMIT 1); sometimes delete 1 row, sometimes 2 rows and sometimes nothing?

If I write it in this form: SET @var = (SELECT id FROM (SELECT * FROM test) temp ORDER BY RAND() LIMIT 1); DELETE FROM test WHERE id=@var;, then it work correctly - is problem in subquery?