MySQL Introductory learning (v)

Source: Internet
Author: User
MySQL MySQL Primer learning (v)
--Multi-table operation

We're familiar with the basics of database and database tables, so let's take a look at how to manipulate multiple tables.

In a database, there may be multiple tables, all of which are interrelated. We continue to use the previous example. The table previously established contains some basic information about the employee, such as name, sex, date of birth, place of birth. We'll create a table that describes the articles published by the employee, including the author's name, the title of the article, and the date of publication.

1, view the contents of the first table mytable:
Mysql> select * FROM MyTable;
+----------+------+------------+-----------+
| name | sex | Birth | birthaddr |
+----------+------+------------+-----------+
| Abccs |f | 1977-07-07 | Our |
| Mary |f | 1978-12-12 | USA |
| Tom |m | 1970-09-02 | USA |
+----------+------+------------+-----------+

2, create a second table title (including author, article title, Publication date):
Mysql> CREATE TABLE title (writer varchar) NOT NULL,
-> title varchar NOT NULL,
-> senddate date);

Add a record to the table, and the final table reads as follows:
Mysql> select * from title;
+--------+-------+------------+
| Writer | Title | Senddate |
+--------+-------+------------+
| Abccs | A1 | 2000-01-23 |
| Mary | B1 | 1998-03-21 |
| Abccs | A2 | 2000-12-04 |
| Tom | C1 | 1992-05-16 |
| Tom | C2 | 1999-12-12 |
+--------+-------+------------+
5 rows in Set (0.00SEC)

3. Multi-Table Query
Now we have two tables: MyTable and title. Using these two tables we can make a combination query:
For example, we want to query the author Abccs's name, sex, article:
Mysql> SELECT Name,sex,title from Mytable,title
-> WHERE name=writer and name= ' Abccs ';
+-------+------+-------+
| name | sex | Title |
+-------+------+-------+
| Abccs | f | A1 |
| Abccs | f | A2 |
+-------+------+-------+

In the example above, because the author's name, sex, and article are recorded in two different tables, you must use a combination to query. You must specify how records in one table match the records in other tables.
Note: If the writer column in the second table title is also named name (the same as the Name column in the MyTable table) rather than writer, it must be represented by Mytable.name and Title.name as a distinction.

Another example is used to query the author, birthplace, and date of birth of the article A2:
Mysql> Select Title,writer,birthaddr,birth from Mytable,title
-> where Mytable.name=title.writer and title= ' A2 ';
+-------+--------+-----------+------------+
| Title | Writer | birthaddr | Birth |
+-------+--------+-----------+------------+
| A2 | Abccs | Our | 1977-07-07 |
+-------+--------+-----------+------------+


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.