MySQL can be inserted into the specified table based on the results of the query

Source: Internet
Author: User


MySQL can be inserted into the specified table based on the set of records it queries to:


First, the structure of three tables is elaborated:

S (sno,sname.sex,age,dept)//Student Information Sheet

C (cno,cname,tname)//Course Information Sheet

SC (Sno,cno,grade)//Course Elective Information sheet


So now the question is:


To insert a student's number, name, and name into an existing (to be established) basic table Stu (Sno,sname,dept) in a class with a score of 80 or higher.

Stu table already exists the database schema is:

<span style= "Font-size:18px;color: #FF0000;" >drop table if EXISTS stu; CREATE table Stu (  Sno Int (4) Unique,  sname varchar (),  dept varchar (TEN));< /SPAN>


Now write the SQL statement for the result:

<span style= "Font-size:18px;color: #FF0000;" >insert into Stu (sno,sname,dept) Select Sno, Sname, dept from S Wheres.sno in (select Sc.sno from SC where Sc.grade &G t;=);</span>

Next is the syntax rule for the SQL statement above:

INSERT into Table name 1 (field List 1)       SELECT field List 2 from table name 2 WHERE conditional expression;
    • Table Name 1: Indicates which table the record is inserted into.
    • Field List 1: Indicates which fields are assigned values.
    • Field List 2: Data that represents which fields are queried from the table.
    • Table Name 2: Indicates which table the record was queried from.
    • Conditional expression: Represents the query condition that sets the SELECT statement.

When you insert a record that is queried in table name 2 into table name 1, the original record is still saved in table name 2.

Tips:

With this approach, you must ensure that the number of fields in field List 1 and field List 2 is the same, and that each corresponding field must have the same data type. If the data type is different, the database system will error. Then, prevent the INSERT statement from executing down.




MySQL can be inserted into the specified table based on the results of the query

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.