SoFunction
Updated on 2025-03-09

Mysql query method to start with a "string"

Mysql query query starting with a "string"

Query strings that do not start with a certain string or string

1. Use the left() function

select * from order where left(id,2)<>"AB";

2. Use like

select * from order where id not like "%AB%";

Query strings starting with one or some strings

1. Use the left() function

select * from order where left(id,2)="AB";

2. Use like

select * from order where id like "%AB%";

Mysql query condition string type = 0

If there is table A

id int
name varchar

There are the following data in Table A

id name
1 Zhang San
2 Li Si
3 2 Kings and Five

Execute the following sql:

select * from A where name = 0;

Will beid=1,id=2The result of .

select * from A where name = 2;

The result with id=3 will be returned.

Why?

Because Mysql "Strings are automatically converted to numbers and numbers to strings as necessary", strings are automatically converted to strings.

When a string is compared with a number, mysql will intercept the number from the beginning of the string, and convert it directly into 0 without the number.

Different types of data are not recommended for comparison.

The above is personal experience. I hope you can give you a reference and I hope you can support me more.