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

有主从结构的数据快速比较的sql怎么写?

  •  
  •   banxi1988 ·
    banxi1988 · Sep 11, 2013 · 3743 views
    This topic created in 4785 days ago, the information mentioned may be changed or developed.
    我有一个对象假设为P
    {
    "name":"banxi",
    "sex":1,
    "childs":[
    {"name":"c1","birth":12},
    {"name":"c2","birth":13},
    ]
    }

    这样的数据在数据库中的结构
    是这样的
    p_table:
    name sex
    ==========================
    banxi 1

    还有一个child的表:
    c_table:
    name birth
    =================================
    c1 12
    c2 15

    怎么样才能用一条sql语句查出是否在数据库中已经有与对象P相同内容的记录了.
    有的话返回.

    我之前的话,
    是根据p_table的name和sex查找出相同的.
    然后在代码里一个个比对child.
    但是这样的效率很不高.因为同name同sex的比较多.
    5 replies  •  1970-01-01 08:00:00 +08:00
    roricon
        1
    roricon  
       Sep 11, 2013
    c_table和b_table是通过什么建立关系的?难道c_table字段里面没有一个类似于_parent的字段作为标示嘛?
    wingoo
        2
    wingoo  
       Sep 11, 2013   ❤️ 1
    搞个指纹吧
    把筛选条件md5一下
    banxi1988
        3
    banxi1988  
    OP
       Sep 11, 2013
    @roricon 忘了写了.
    c_table中有一个pid引用了p_table的主键...
    banxi1988
        4
    banxi1988  
    OP
       Sep 11, 2013
    @wingoo
    我刚想了下,因为childs的个数的有限的平均在2-5个左右.
    所以,将childs中的字段拼接起来在p_table中作为一个新的字段c_checksums
    查的时候只需要比较p_table中c_checksums就可以知道其子childs是不是相同了,这样就不用写复杂的sql了.
    wingoo
        5
    wingoo  
       Sep 11, 2013
    @banxi1988 恩, 拼接后再做个md5就行了, 要不字段可能会长一些
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1138 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 23:44 · PVG 07:44 · LAX 16:44 · JFK 19:44
    ♥ Do have faith in what you're doing.