dongzhuandian3292 2017-12-05 17:29
浏览 32

将MySQL转换为新系统?

I have an old MySQL table structure that I want to upgrade to a new one, I'll explain the structures below.

Its for a permission system, and the table represents what rank id's get what permission id's

Table [Old]:
permission_id the id of the permission given
rank the rank id that gets the permission

Now, for the old table system, it uses an extra call to permissions table to find the name of the permission linked with permission_id, in my new system I just store permission rights by permission's name not the permissions id.

Table [New]:
permission_name the name of the permission given
allowed_ranks a string of rank ids, seperated by ,

How would I convert all the records to 1 simple record for all permissions, but convert the rank column to the new allowed_ranks with a seperator of ,?

  • 写回答

1条回答 默认 最新

  • dongqian8265 2017-12-05 22:09
    关注

    Do not convert to the new system if you have any choice. You can avoid "an extra call to permissions" by just joining the tables.

    SELECT * 
    FROM old_table AS t 
    INNER JOIN permissions AS p ON t.permission_id = p.permission_id 
    WHERE t.rank = ?
    ;
    

    If you need to display the information in the new table format, just use this query:

    SELECT p.permission_name, GROUP_CONCAT(t.rank) AS allowed_ranks
    FROM old_table AS t 
    INNER JOIN permissions AS p ON t.permission_id = p.permission_id 
    GROUP BY p.permission_name
    ;
    

    Note: you could use the second query to populate the new table, but it is pretty much guaranteed you will quickly regret moving to it; at least, once you have to find all permissions associated with a specific rank.

    评论

报告相同问题?

悬赏问题

  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭