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

求助 mysql & 运算符是什么意思, 怎样解释

  •  
  •   gyinbj · Apr 8, 2020 · 5407 views
    This topic created in 2386 days ago, the information mentioned may be changed or developed.

    select id from Table where id = '888888' and (ext_type & 4 = 4 )

    这句话后面的(ext_type & 4 = 4 ) 要怎样解释?

    数据存储是 1 2 4 8 的和 15

    用于判断 1 2 4 8 是否符合这 4 个权限

    9 replies  •  2020-04-09 10:59:15 +08:00
    Presbyter
        1
    Presbyter  
       Apr 8, 2020
    关键词“8421 码”,“逻辑运算”.
    gyinbj
        2
    gyinbj  
    OP
       Apr 8, 2020
    @Presbyter 没太懂。
    PTLin
        3
    PTLin  
       Apr 8, 2020
    判断第三位是否被置位
    no1xsyzy
        4
    no1xsyzy  
       Apr 8, 2020
    位运算作 bit test
    老的 C 甚至是 asm 的做法了
    caola
        5
    caola  
       Apr 8, 2020
    位运算符:& 按位与, | 按位或, ^ 按位异或
    otakustay
        6
    otakustay  
       Apr 8, 2020
    这是 bit enum 或者 flag enum,搜一下能找到比较详尽的资料
    dapang1221
        7
    dapang1221  
       Apr 8, 2020
    那个 4 换成二进制是 0100,和 ext_type 做与运算,相当于把 ext_type 的第三个 bit 位提取出来,再判断这一位是不是 1

    不过 mysql 位运算会全表扫描,这条 sql 有限定 id 了倒是不会全表扫
    shaoyijiong
        8
    shaoyijiong  
       Apr 8, 2020
    字面含义二进制计算 得到的结果十进制后再比较
    0100 (4)
    &
    0100 (4)
    -----
    0100 (4)
    Aresxue
        9
    Aresxue  
       Apr 9, 2020
    把十进制转成二进制就看出规律了, 0100,相当于取第三位, 这个做法比较老但还算常见, linux 的常用权限 755, java 里面对于修饰符的判断都有用到
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   5630 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 57ms · UTC 01:39 · PVG 09:39 · LAX 18:39 · JFK 21:39
    ♥ Do have faith in what you're doing.