V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
Dlin
V2EX  ›  MySQL

mysql5 的一个 in 关键字使用的疑惑

  •  
  •   Dlin · 242 天前 · 1039 次点击
    这是一个创建于 242 天前的主题,其中的信息可能已经有所发展或是发生改变。
    select * from esign_contract where task_item_id in ('1635,1636')
    

    这个 sql 为什么能且只能匹配出 task_item_id = 1635 的记录。按我理解'1635,1636'是一个完整的字符串,不应该匹配出 1635 的记录。另外如果 in 关键字会对字符串中有',' (尝试了使用中文逗号,或符号,空格都会有这个效果)做拆分的话,那么也应该匹配出 1636 的结果,但是并没有匹配出来 1636 的结果

    版本是 5.7.38-220701-log

    7 条回复    2023-08-29 13:59:21 +08:00
    cheng6563
        1
    cheng6563  
       242 天前
    你 task_item_id 是不是数字类型。。。
    Dlin
        2
    Dlin  
    OP
       242 天前
    @cheng6563 是的
    hidemyself
        3
    hidemyself  
       242 天前
    我猜,应该是自动类型转换了。
    '1635,1636' 被转换成了 1635
    setsunakute
        4
    setsunakute  
       242 天前
    对于你这种场景,你应该用 find_in_set ,而不是 in
    liprais
        5
    liprais  
       242 天前
    @Dlin 人家都告诉你了,你猜'1635,1636'转成数字类型出来的是啥
    8355
        6
    8355  
       242 天前
    mysql 隐式转换
    https://dev.mysql.com/doc/refman/8.0/en/type-conversion.html
    SELECT CAST('78,55' AS UNSIGNED)
    task_item_id 是 int 类型
    传入的是字符串类型所以会将字符串转为 int
    Dlin
        7
    Dlin  
    OP
       242 天前
    @hidemyself
    @setsunakute
    @liprais
    @8355
    好的明白了,感谢解疑
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2746 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 10:20 · PVG 18:20 · LAX 03:20 · JFK 06:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.