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