萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> mysql教程 >> mysql聯合查詢視圖實現方法

mysql聯合查詢視圖實現方法

mysql教程聯合查詢視圖實現方法

mysql>
mysql> create table employee(
    ->     id            int,
    ->     first_name    varchar(15),
    ->     last_name     varchar(15),
    ->     start_date    date,
    ->     end_date      date,
    ->     salary        float(8,2),
    ->     city          varchar(10),
    ->     description   varchar(15)
    -> );
query ok, 0 rows affected (0.02 sec)

mysql>
mysql> create table job (
    ->   id         int,
    ->   title      varchar(20)
    -> );
query ok, 0 rows affected (0.06 sec)

mysql>
mysql> insert into employee(id,first_name, last_name, start_date, end_date,   salary,  city,       description)
    ->              values (1,'jason',    'martin',  '19960725',  '20060725', 1234.56, 'toronto',  'programmer');
query ok, 1 row affected (0.00 sec)

mysql>
mysql> insert into employee(id,first_name, last_name, start_date, end_date,   salary,  city,       description)
    ->               values(2,'alison',   'mathews',  '19760321', '19860221', 6661.78, 'vancouver','tester');
query ok, 1 row affected (0.00 sec)

mysql>
mysql> insert into employee(id,first_name, last_name, start_date, end_date,   salary,  city,       description)
    ->               values(3,'james',    'smith',    '19781212', '19900315', 6544.78, 'vancouver','tester');
query ok, 1 row affected (0.00 sec)

mysql>
mysql> insert into employee(id,first_name, last_name, start_date, end_date,   salary,  city,       description)
    ->               values(4,'celia',    'rice',     '19821024', '19990421', 2344.78, 'vancouver','manager');
query ok, 1 row affected (0.00 sec)

mysql>
mysql> insert into employee(id,first_name, last_name, start_date, end_date,   salary,  city,       description)
    ->               values(5,'robert',   'black',    '19840115', '19980808', 2334.78, 'vancouver','tester');
query ok, 1 row affected (0.01 sec)

mysql>
mysql> insert into employee(id,first_name, last_name, start_date, end_date,   salary,  city,       description)
    ->               values(6,'linda',    'green',    '19870730', '19960104', 4322.78,'new york',  'tester');
query ok, 1 row affected (0.00 sec)

mysql>
mysql> insert into employee(id,first_name, last_name, start_date, end_date,   salary,  city,       description)
    ->               values(7,'david',    'larry',    '19901231', '19980212', 7897.78,'new york',  'manager');
query ok, 1 row affected (0.00 sec)

mysql>
mysql> insert into employee(id,first_name, last_name, start_date, end_date,   salary,  city,       description)
    ->               values(8,'james',    'cat',     '19960917',  '20020415', 1232.78,'vancouver', 'tester');
query ok, 1 row affected (0.00 sec)

mysql>
mysql> insert into job (id, title) values (1,'tester');
query ok, 1 row affected (0.01 sec)

mysql> insert into job (id, title) values (2,'accountant');
query ok, 1 row affected (0.00 sec)

mysql> insert into job (id, title) values (3,'developer');
query ok, 1 row affected (0.00 sec)

mysql> insert into job (id, title) values (4,'coder');
query ok, 1 row affected (0.00 sec)

mysql> insert into job (id, title) values (5,'director');
query ok, 1 row affected (0.00 sec)

mysql> insert into job (id, title) values (6,'mediator');
query ok, 1 row affected (0.00 sec)

mysql> insert into job (id, title) values (7,'proffessor');
query ok, 1 row affected (0.00 sec)

mysql> insert into job (id, title) values (8,'programmer');
query ok, 1 row affected (0.02 sec)

mysql> insert into job (id, title) values (9,'developer');
query ok, 1 row affected (0.00 sec)

mysql>
mysql> select * from job;
+------+------------+
| id   | title      |
+------+------------+
|    1 | tester     |
|    2 | accountant |
|    3 | developer  |
|    4 | coder      |
|    5 | director   |
|    6 | mediator   |
|    7 | proffessor |
|    8 | programmer |
|    9 | developer  |
+------+------------+
9 rows in set (0.00 sec)

mysql> select * from employee;
+------+------------+-----------+------------+------------+---------+-----------+-------------+
| id   | first_name | last_name | start_date | end_date   | salary  | city      | description |
+------+------------+-----------+------------+------------+---------+-----------+-------------+
|    1 | jason      | martin    | 1996-07-25 | 2006-07-25 | 1234.56 | toronto   | programmer  |
|    2 | alison     | mathews   | 1976-03-21 | 1986-02-21 | 6661.78 | vancouver | tester      |
|    3 | james      | smith     | 1978-12-12 | 1990-03-15 | 6544.78 | vancouver | tester      |
|    4 | celia      | rice      | 1982-10-24 | 1999-04-21 | 2344.78 | vancouver | manager     |
|    5 | robert     | black     | 1984-01-15 | 1998-08-08 | 2334.78 | vancouver | tester      |
|    6 | linda      | green     | 1987-07-30 | 1996-01-04 | 4322.78 | new york  | tester      |
|    7 | david      | larry     | 1990-12-31 | 1998-02-12 | 7897.78 | new york  | manager     |
|    8 | james      | cat       | 1996-09-17 | 2002-04-15 | 1232.78 | vancouver | tester      |
+------+------------+-----------+------------+------------+---------+-----------+-------------+
8 rows in set (0.00 sec)

mysql>
mysql>
mysql> create algorithm = temptable view myview
    -> (id, first_name, title) as
    -> select o.id, o.first_name, c.title
    -> from job c, employee o
    -> where o.id = c.id;
query ok, 0 rows affected (0.00 sec)

mysql>
mysql> select * from myview;
+------+------------+------------+
| id   | first_name | title      |
+------+------------+------------+
|    1 | jason      | tester     |
|    2 | alison     | accountant |
|    3 | james      | developer  |
|    4 | celia      | coder      |
|    5 | robert     | director   |
|    6 | linda      | mediator   |
|    7 | david      | proffessor |
|    8 | james      | programmer |
+------+------------+------------+
8 rows in set (0.00 sec)

mysql>
mysql> drop view myview;
query ok, 0 rows affected (0.00 sec)

mysql>
mysql>
mysql>
mysql>
mysql> drop table job;
query ok, 0 rows affected (0.02 sec)

mysql> drop table employee;
query ok, 0 rows affected (0.00 sec)

mysql>

copyright © 萬盛學電腦網 all rights reserved