8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

MySQL 8.4 参考手册 / 5 教程 / 5.6 常见查询示例
中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

5.6 常见查询示例5.6 Examples of Common Queries

以下是一些解决 MySQL 常见问题的示例。Here are examples of how to solve some common problems with MySQL.

一些示例使用表 shop 来保存某些商人(dealer)的每件商品(item number)的价格。假设每个商人对每件商品只有一个固定价格,那么 (article, dealer) 就是记录的主键。Some of the examples use the table shop to hold the price of each article (item number) for certain traders (dealers). Supposing that each trader has a single fixed price per article, then (article, dealer) is a primary key for the records.

启动命令行工具 mysql 并选择一个数据库:Start the command-line tool mysql and select a database:

$> mysql your-database-name

要创建并填充示例表,请使用以下语句:To create and populate the example table, use these statements:

CREATE TABLE shop (
    article INT UNSIGNED  DEFAULT '0000' NOT NULL,
    dealer  CHAR(20)      DEFAULT ''     NOT NULL,
    price   DECIMAL(16,2) DEFAULT '0.00' NOT NULL,
    PRIMARY KEY(article, dealer));
INSERT INTO shop VALUES
    (1,'A',3.45),(1,'B',3.99),(2,'A',10.99),(3,'B',1.45),
    (3,'C',1.69),(3,'D',1.25),(4,'D',19.95);

执行这些语句后,表应具有以下内容:After issuing the statements, the table should have the following contents:

SELECT * FROM shop ORDER BY article;

+---------+--------+-------+
| article | dealer | price |
+---------+--------+-------+
|       1 | A      |  3.45 |
|       1 | B      |  3.99 |
|       2 | A      | 10.99 |
|       3 | B      |  1.45 |
|       3 | C      |  1.69 |
|       3 | D      |  1.25 |
|       4 | D      | 19.95 |
+---------+--------+-------+