mysql where exact match

后端 未结 4 876
梦谈多话
梦谈多话 2021-01-14 04:47

When i write this query

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

return

title            city  
--         


        
4条回答
  •  佛祖请我去吃肉
    2021-01-14 05:03

    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.)

提交回复
热议问题