Mysql Getting Started (5) Multi-Table Operation (Add Time: 2002-3-25 Hits: 3441)
ABCCS
Multi-table operation We are familiar with the basic operations of the database and database tables, now let's take a look at how to operate multiple tables. In a database, there may be multiple tables that are interrelated. We continue to use the previous example. The previously established table contains some basic information of the employee, such as name, gender, date of birth, birthplace. We create a table again that describe the article published by the employee, including the author's name, article title, and date. 1. View the first table myTable content: mysql> select * from mytable; -------- ---- ------------ ----------- | Name | SEX | BIRTH | BIRTHADDR | ---------- ------ -------- - ----------- | ABCCS | F | 1977-07-07 | China | | MARY | F | 1978-12-12 | USA | | Tom | M | 1970-09- 02 | USA | ---------- ------ ---------- ---------- 2, Create a second table title (including the author, article title, date): mysql> create table title (Writer varchar (20) Not null, -> Title Varchar (40) Not Null, -> Senddate Date; to the table Fill in the record, the contents of the last table are 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 must check the name, gender, article: mysql> select name, sex, title from myTable, title-> where name = write, title -> where name = write - ------- ------ ------- | Name | SEX | TITLE | ------- ---- -- --- | ABCCS | F | A1 | | ABCCS | F | A2 | ------- ---- ------- The above example, due to the author's name, Gender, article records in two different tables, so a combination must be used for query. You must specify how records in a table matches the records in other tables. Note: If the Writer column in the second table title is also named Name (same as the name column in the MyTable table) instead of Writer, you must use myTable.name and title.name to distinguish it.