MySQL single statement to merge two tables

前端 未结 5 895
傲寒
傲寒 2021-01-24 04:01

I\'m sure this has been ask/answered already but I don\'t know how this kind of action is called and my SQL knowledge is limited.

I\'m searching for a single SQL statem

5条回答
  •  花落未央
    2021-01-24 04:42

    Generally this isn't considered as a good practice for flat type tables (like user data). This structure is usually used for storing user specified custom fields or meta data where you can have none or hundreds of them for a single instance.

    The reason for avoiding this is if you want to get N user property you have to do N-1 join in MySQL and probably dynamically generate the query in the application which is suboptimal. Just put all your standard data in one table and you can use this user_field table for custom fields which only retrieved upon request.

    If you're stuck with this structure to be able to do the querying efficiently I would change the user_field table to not have auto_increment id and rather make the user_id and key the PRIMARY KEY for the table. This way all the fields for a user will end up next to each other in the table space (assuming you're using InnoDB) and the lookup will be a primary key lookup. One further step would be to change the key to be tinyint and use lookup IDs instead of actual characters (you can even use CONSTANTS in the code to make it cleaner).

提交回复
热议问题