当前位置:网站首页>MySQL inserts query results into other tables

MySQL inserts query results into other tables

2022-06-10 21:36:00 Dark horse programmer official

Learning goals

  • Be able to write and insert query results into other tables SQL sentence

1. reflection

There is only one goods surface , We want to add a product classification information , such as : Mobile device classification information , Only pass goods The table cannot complete the addition of commodity classification , So how to add commodity classification information ?

answer :

  1. Create a commodity classification table , hold goods The commodity classification information in the table is added to the table .
  2. take goods The classification name in the table is changed to the corresponding classification in the commodity classification table id

2. Create commodity classification table

--  Create commodity classification table 
create table good_cates(
    id int not null primary key auto_increment, 
    name varchar(50) not null
);

3. hold goods The commodity classification in the table is added to the commodity classification table

--  Inquire about goods Classification information of commodities in the table 
select cate_name from goods group by cate_name;

--  Insert the query results into good_cates In the table 
insert into good_cates(name) select cate_name from goods group by cate_name;

--  Add mobile device classification information 
insert into good_cates(name) values(' Mobile devices ');

explain :

  • insert into .. select .. Express : Insert the query results into the specified table , That is, table replication .

4. Summary

  • To complete table replication, you can use : insert into .. select .. SQL sentence

MySQL Basic use of database
Understand the role of the database 、 Features and relational database management system MySQL Introduction to database installation Data types and constraints Command line client MySQL Use
as and distinct keyword SQL sentence —where Conditions of the query MySQL Sorting Query and paging query
MySQL Conditional query of database
mysql Common aggregate functions in Mysql Group query MySQL Connection query inside connection 、 Left connection 、 The right connection 、 Self join MySQL Introduction and use of subquery
The third paradigm of database design Foreign keys SQL Statement writing rehearse - A combination of grouping and aggregation functions
MySQL Advanced use of databases
Updating ……
原网站

版权声明
本文为[Dark horse programmer official]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206102026562141.html