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
iscraft
V2EX  ›  MySQL

varchar 和 int 不能同时针对一个字符串值进行精确查询?!

  •  
  •   iscraft · Sep 27, 2017 · 3558 views
    This topic created in 3175 days ago, the information mentioned may be changed or developed.
    CREATE TABLE `user` (
     `id` int(10) NOT NULL AUTO_INCREMENT,
     `real_name` varchar(10) NOT NULL,
     `tel` varchar(25) DEFAULT NULL,
     `rid` int(10) DEFAULT NULL,
     `status` tinyint(1) NOT NULL,
     PRIMARY KEY (`id`)
    ) ENGINE=MyISAM AUTO_INCREMENT=207 DEFAULT CHARSET=utf8
    

    原意是用户在一个 input 里面输入姓名或者电话或者证件号码都能查询登录

    使用

    SELECT * FROM `user` WHERE (real_name='刘大军' or tel='刘大军' or `rid`='刘大军') and status='1'    
    

    会获取到连同正确的那条在内的一共 3 条记录 结果不唯一 显然不对

    而使用

    SELECT * FROM `user` WHERE (`real_name` like '%刘大军%' or `tel` like '%刘大军%' or `rid` like '%刘大军%') and `status`='1'    
    

    则能获取唯一的一条正确的记录

    但是感觉 like 不是很严谨
    网上搜索了好一阵子
    也找不到 varchar 和 int 针对一个字符串值精确匹配的相关文章
    虽然我也知道 int 是面向整数的
    第一条语句怎么写才能准确的用 real_name 和 tel 匹配姓名字符串 而使用 rid 来匹配纯数字的证件号呢?
    求解惑

    1 replies    2017-09-27 23:00:41 +08:00
    msg7086
        1
    msg7086  
       Sep 27, 2017
    if rid 是数字
    select 三个条件
    else
    select 两个条件
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5484 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 07:46 · PVG 15:46 · LAX 00:46 · JFK 03:46
    ♥ Do have faith in what you're doing.