文档主页
适用于 VS Code 的 MySQL Shell


适用于 VS Code 的 MySQL Shell  /  SQL  /  联接表、更新记录和插入值

预发布版本:2024-07-17

6.3 联接表、更新记录和插入值

联接 sakila.address、sakila.city、sakila.country 和 sakila.customer 表

SELECT cus.last_name AS 'Last Name', ad.address, ci.city AS 'City', ctr.country 
FROM sakila.customer cus
JOIN sakila.address ad ON (cus.address_id = ad.address_id)
JOIN sakila.city ci ON (ad.city_id = ci.city_id)
JOIN sakila.country ctr ON (ci.country_id = ctr.country_id)
WHERE ctr.country = 'canada' OR ctr.country = 'algeria'
ORDER BY ctr.country ASC;

输出为

+-----------+-------------------------+---------------+---------+
| Last Name | address                 | City          | country |
+-----------+-------------------------+---------------+---------+
| CHEATHAM  | 1924 Shimonoseki Drive  | Batna         | Algeria |
| GRAY      | 1031 Daugavpils Parkway | Bchar         | Algeria |
| CARROLL   | 757 Rustenburg Avenue   | Skikda        | Algeria |
| BOURQUE   | 1153 Allende Way        | Gatineau      | Canada  |
| POWER     | 1844 Usak Avenue        | Halifax       | Canada  |
| CARPENTER | 891 Novi Sad Manor      | Oshawa        | Canada  |
| IRBY      | 432 Garden Grove Street | Richmond Hill | Canada  |
| QUIGLEY   | 983 Santa F Way         | Vancouver     | Canada  |
+-----------+-------------------------+---------------+---------+
OK, 8 records retrieved in 3.092ms

更新 sakila.actor 中的单个记录

-- error in the following entry
-- +----------+-------------+--------------+---------------------+
-- | actor_id | first_name  | last_name    | last_update         |
-- |        8 | MATTHEW     | JOHANSSON    | 2006-02-15 04:34:33 |
UPDATE sakila.actor
SET first_name = 'John', last_name = 'Matthewson'
WHERE actor_id = 8;

将值插入 sakila.actor

INSERT INTO sakila.actor (first_name, last_name)
  VALUES 
  ('Jimmy', 'Smith'),
  ('Bob', 'Roberts');