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

当某列值有更名时,该怎样做比较好?

  •  
  •   billgreen1 · Jan 8, 2016 · 3532 views
    This topic created in 3791 days ago, the information mentioned may be changed or developed.
    我有一个表 table1 ,格式如下
    id, company, col1,col2,...

    company 里面有同一个公司发生更名情况。
    比如 companyA 在 2010-01-01 以前 名字叫 abc , 2010-01-01 更名为 xyz 。

    这样的公司可能有不少。
    table1 的记录大概有一亿条左右, 10G+.
    不会存在其他 也叫 abc/xyz 的情况。


    场景: boss 说把 xyz 公司的 2000 年至今的数据调出来。或者把 abc 公司的 2000 年至今的数据调出来。
    直接 select * from table1 where company = 'xyz' and date between date1 and date2
    是不行的。

    我的想法是另外做一张表 table2 ,
    id, company, all_alias
    1, abc, (abc,xyz)
    2, xyz, (abc,xyz)

    先从 table2 中查到其所有用过的名字,
    然后用 select * from table1 where company in (select all_alias from table2 where company='abc')
    and date between date1 and date2

    请问这样做可行否,有没有什么更好的办法?
    10 replies    2016-01-08 13:26:05 +08:00
    lichao
        1
    lichao  
       Jan 8, 2016
    只能说你们的数据库一开始设计得太业余,一开始就应该有一个 company 表, table1 中应该存 companyID ,而不是 companyName
    luban
        2
    luban  
       Jan 8, 2016 via iPhone
    @lichao 现在做也可以,但是程序要改,原来那个字段就当冗余
    imn1
        3
    imn1  
       Jan 8, 2016
    深发展改平安银行?
    理应 company 表
    bk201
        4
    bk201  
       Jan 8, 2016
    加一列 id ,然后打标记不行吗
    wmjie
        5
    wmjie  
       Jan 8, 2016
    select * from table1 where company in ('xyz', 'xyz ' ) and date between date1 and date2
    Alphabetcn
        6
    Alphabetcn  
       Jan 8, 2016
    数据库太业余
    billgreen1
        7
    billgreen1  
    OP
       Jan 8, 2016
    @lichao, 恩,是太业余,我自己包括周围的人都喜欢一张表搞定,本身列数目就不多。
    billgreen1
        8
    billgreen1  
    OP
       Jan 8, 2016
    @Alphabetcn 赞同加没有帮助,:-)
    billgreen1
        9
    billgreen1  
    OP
       Jan 8, 2016
    @imn1, 嘻嘻,不是的,举公司的例子只是为了方便描述我的问题。
    billgreen1
        10
    billgreen1  
    OP
       Jan 8, 2016
    @luban 能稍微详细说点么,多谢啦,你的回答是最有帮助的。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2659 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 48ms · UTC 15:42 · PVG 23:42 · LAX 08:42 · JFK 11:42
    ♥ Do have faith in what you're doing.