Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why 3 rows deleted sql query puzzle?

Tags:

sql

mysql

can anybody tell me the technical reason for this?

CREATE TABLE test (
  id varchar(3) NOT NULL,
  PRIMARY KEY  (id)
);

INSERT INTO test VALUES ('0'), ('1'), ('2'), ('ab'), ('bb');

select * from test;

DELETE FROM test WHERE id=0;

Which Deletes 3 rows from table test.
like image 935
user699235 Avatar asked Aug 02 '26 08:08

user699235


1 Answers

Because this:

SELECT 0 = 'ab';
+----------+
| 0 = 'ab' |
+----------+
|        1 |
+----------+

To delete exact rows use BINARY operator, it will force byte by byte comparison -

DELETE FROM test WHERE id = BINARY 0;
like image 65
Devart Avatar answered Aug 04 '26 01:08

Devart



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!