mysql where match_mysql where exact match

在 MySQL 查询中,当尝试使用字符串与 INT 字段进行精确匹配时,可能会遇到类型转换问题导致不预期的结果。问题在于 MySQL 会尝试将字符串转换为整数进行比较。解决方法包括显式将 ID 转换为字符串或者使用 LIKE 操作符进行模糊匹配。确保在查询前验证输入的 ID 是否为纯数字,避免混合数字和字母的比较,以保证 INT 字段的精确匹配。
摘要由CSDN通过智能技术生成

问题

When i write this query

SELECT cd.title, cd.city FROM coupon_detail cd WHERE cd.id = 260;

return

title city

--------------- ------

Butterfly world Mohali

and when i write

SELECT cd.title, cd.city FROM coupon_detail cd WHERE cd.id = '260abcxyz';

return

title city

--------------- ------

Butterfly world Mohali

see the second one is wrong. i want to get the value only IF ID EXACTLY MATCH ... how to solve this. thanks for your help.

回答1:

You could convert the id to a string so the comparison will be done exactly. You could use LIKE to cause an implicit conversion

SELECT cd.title, cd.city FROM coupon_detail cd WHERE cd.id LIKE '260abcxyz';

or alternative, you can perform the cast explicitly

SELECT cd.title, cd.city FROM coupon_detail cd WHERE CAST(cd.id AS CHAR) = '260abcxyz';

However, if all your IDs are integers, it's probably more appropriate to check these values before you try to query the database. (If the ID you're querying with isn't a number, there can't be a match anyway.)

回答2:

This is happening because of Type Conversion in MySql. My sql treats '260abcxyz' as an integer in your query and AFAIK because first char is number MySql casts it to a number and it becomes only 260 and that's why you are getting result. If you write character first like 'abcxyz260' it will successes the comparison.

More explanation available here: Type Conversion in MySql

As a solution: you should take care that only numbers are passed in comparison and not combination of string and number.

回答3:

Well, you should not use quote if the id is INT. The way MySQL does is if you use String to compare a INT, it uses just the valid initial integer from start.

see this:

mysql> desc coupon_detail;

+-------+--------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------+--------------+------+-----+---------+-------+

| id | int(11) | YES | | NULL | |

| title | varchar(100) | YES | | NULL | |

| city | varchar(100) | YES | | NULL | |

+-------+--------------+------+-----+---------+-------+

3 rows in set (0.04 sec)

mysql> SELECT cd.title, cd.city FROM coupon_detail cd WHERE cd.id = 'ff260';

Empty set, 1 warning (0.00 sec)

mysql> SELECT cd.title, cd.city FROM coupon_detail cd WHERE cd.id = '260ff';

+-----------------+--------+

| title | city |

+-----------------+--------+

| Butterfly world | Mohali |

+-----------------+--------+

1 row in set, 1 warning (0.00 sec)

mysql> SELECT cd.title, cd.city FROM coupon_detail cd WHERE cd.id = 260;

+-----------------+--------+

| title | city |

+-----------------+--------+

| Butterfly world | Mohali |

+-----------------+--------+

1 row in set (0.00 sec)

mysql> SELECT cd.title, cd.city FROM coupon_detail cd WHERE cd.id = 26011;

Empty set (0.00 sec)

The idea is not to mix numbers and aphabets to compare a INT column. If you have to you may consider

converting ID column to VARCHAR. In that case auto-increment may go away.

have application logic to query only the strings that has numerics.

回答4:

use like instead = because if the string is too long than exact match won't work in that way

SELECT cd.title, cd.city FROM coupon_detail cd WHERE cd.id LIKE '260abcxyz';

来源:https://stackoverflow.com/questions/8570884/mysql-where-exact-match

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值