See all database tables referring to the address: https://www.cnblogs.com/zhoulixiangblog/p/12078724.html
Database tables used herein:
prod_id | vend_id | prod_name | prod_price | prod_desc |
---|---|---|---|---|
ANV01 | 1001 | .5 ton anvil | 5.99 | .5 ton anvil, black, complete with handy hook |
ANV02 | 1001 | 1 ton anvil | 9.99 | 1 ton anvil, black, complete with handy hook and carrying case |
ANV03 | 1001 | 2 ton anvil | 14.99 | 2 ton anvil, black, complete with handy hook and carrying case |
Dtntri | 1003 | Detonator | 13 | Detonator (plunger powered), fuses not included |
FB | 1003 | Bird seed | 10 | Large bag (suitable for road runners) |
FC | 1003 | Carrots | 2.5 | Carrots (rabbit hunting season only) |
FU1 | 1002 | Fuses | 3.42 | 1 dozen, extra long |
JP1000 | 1005 | JetPack 1000 | 35 | JetPack 1000, intended for single use |
JP2000 | 1005 | JetPack 2000 | 55 | JetPack 2000, multi-use |
OL1 | 1002 | Oil can | 8.99 | Oil can, red |
SAFE | 1003 | Safe | 50 | Safe with combination lock |
SLING | 1003 | Sling | 4.49 | Sling, one size fits all |
NT1 | 1003 | TNT (1 stick) | 2.5 | TNT, red, single stick |
TNT2 | 1003 | TNT (5 sticks) | 10 | TNT, red, pack of 10 sticks |
1. Find a single column
Input:
SELECT prod_id FROM products;
Output:
prod_id |
---|
ANV01 |
ANV02 |
ANV03 |
Dtntri |
FB |
FC |
FU1 |
JP1000 |
JP2000 |
OL1 |
SAFE |
SLING |
NT1 |
TNT2 |
analysis:
The above statement using a SELECT statement to retrieve the products table in a column named prod_id. Required column names after the SELECT keyword is given, FROM keyword indicates the name of the table from which to retrieve data.
2. Find the multiple columns
Input:
SELECT prod_id, prod_name, prod_price FROM products;
Output:
prod_id | prod_name | prod_price |
---|
analysis:
As in the previous example, this statement using a SELECT statement to select data from the products table. In this example, three columns were specified, column names separated by commas.
3. Find all the columns
Input:
SELECT * FROM products;
Output:
prod_id | vend_id | prod_name | prod_price | prod_desc |
---|---|---|---|---|
ANV01 | 1001 | .5 ton anvil | 5.99 | .5 ton anvil, black, complete with handy hook |
ANV02 | 1001 | 1 ton anvil | 9.99 | 1 ton anvil, black, complete with handy hook and carrying case |
ANV03 | 1001 | 2 ton anvil | 14.99 | 2 ton anvil, black, complete with handy hook and carrying case |
Dtntri | 1003 | Detonator | 13 | Detonator (plunger powered), fuses not included |
FB | 1003 | Bird seed | 10 | Large bag (suitable for road runners) |
FC | 1003 | Carrots | 2.5 | Carrots (rabbit hunting season only) |
FU1 | 1002 | Fuses | 3.42 | 1 dozen, extra long |
JP1000 | 1005 | JetPack 1000 | 35 | JetPack 1000, intended for single use |
JP2000 | 1005 | JetPack 2000 | 55 | JetPack 2000, multi-use |
OL1 | 1002 | Oil can | 8.99 | Oil can, red |
SAFE | 1003 | Safe | 50 | Safe with combination lock |
分析:
如果给定一个通配符(*),则返回表中所有列。列的顺序一般是列在表定义中出现的顺序。但有时候并不是这样的,表的模式的变化(如添加或删除列)可能会导致顺序的变化。
4.查找不同行(去除重复结果)
输入:
SELECT DISTINCT vend_id FROM products;
输出:
vend_id |
---|
1001 |
1002 |
1003 |
1005 |
分析:
SELECT DISTINCT vend_id告诉MySQL只返回不同(唯一)的 vend_id行,因此只返回4行,如上面的输出所示。如果使用 DISTINCT关键字,它必须直接放在列名的前面。
5.限制查找结果
输入:
SELECT prod_name FROM products LIMIT 5;
输出:
prod_name |
---|
.5 ton anvil |
1 ton anvil |
2 ton anvil |
Detonator |
Bird seed |
分析:
此语句使用SELECT语句检索单个列。LIMIT 5指示MySQL返回不多于5行。
输入:
SELECT prod_name FROM products LIMIT 5, 5;
输出:
prod_name |
---|
Carrots |
Fuses |
JetPack 1000 |
JetPack 2000 |
Oil can |
分析:
LIMIT 5, 5指示MySQL返回从行5开始的5行(表的数据从行0开始)。第一个数为开始位置,第二个数为要检索的行数。
6.使用带表名限定的查找
输入:
SELECT products.prod_name FROM products;
输出:
prod_name |
---|
.5 ton anvil |
1 ton anvil |
2 ton anvil |
Detonator |
Bird seed |
Carrots |
Fuses |
JetPack 1000 |
JetPack 2000 |
Oil can |
Safe |
分析:
此条语句在所要查找的列名前面加上了表的名字,进行了限定,查找的结果为products表中的prod_name列。