Reputation: 11
This is the table:
username: id:
john 1
john 2
john 56
john 75
john 98
Now I want query and delete one of these rows randomly and keep the other four- assuming I do not know the value of "ID" because it was Auto-Incremented.
Here's the code, I'm not sure what to add so it only deletes one of the rows associated with 'john'
$query = ("DELETE FROM table WHERE username='$name' && id=''");
How do I modify this so it deletes one row with the name john
and not all of them?
Fixed Code
Simple solution that worked.
$query = ("DELETE FROM table WHERE username='$name' LIMIT 1");
Upvotes: 1
Views: 454
Reputation: 231
I'm not sure if its possible, but perhaps LIMIT the result to 1?
$query = ("DELETE FROM table WHERE username='$name' LIMIT 1");
Upvotes: 0
Reputation: 905
I believe this will work:
$name = 'john';
$query = ("DELETE FROM table WHERE id = (SELECT id FROM table WHERE username = '$name' ORDER BY RAND() LIMIT 1)");
Upvotes: 0