码迷,mamicode.com
首页 > 数据库 > 详细

SQL语句--查找数据select

时间:2019-12-22 00:41:55      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:included   log   uitable   name   com   mysql   cas   ati   mit   

查看全部数据库表参照地址:https://www.cnblogs.com/zhoulixiangblog/p/12078724.html

本文所用数据库表:

 

prod_idvend_idprod_nameprod_priceprod_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
DTNTR 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
TNT1 1003 TNT (1 stick) 2.5 TNT, red, single stick
TNT2 1003 TNT (5 sticks) 10 TNT, red, pack of 10 sticks

1.查找单个列

输入:

SELECT prod_id
FROM products;

输出:

 

prod_id
ANV01
ANV02
ANV03
DTNTR
FB
FC
FU1
JP1000
JP2000
OL1
SAFE
SLING
TNT1
TNT2

分析:

上述语句利用SELECT 语句从products表中检索一个名为 prod_id的列。所需的列名在SELECT 关键字之后给出,FROM 关键字指出从其中检索数据的表名。

2.查找多个列

输入:

SELECT prod_id, prod_name, prod_price
FROM products;

输出:

 

prod_idprod_nameprod_price
ANV01 .5 ton anvil 5.99
ANV02 1 ton anvil 9.99
ANV03 2 ton anvil 14.99
DTNTR Detonator 13
FB Bird seed 10
FC Carrots 2.5
FU1 Fuses 3.42
JP1000 JetPack 1000 35
JP2000 JetPack 2000 55
OL1 Oil can 8.99
SAFE Safe 50
SLING Sling 4.49
TNT1 TNT (1 stick) 2.5
TNT2 TNT (5 sticks) 10

与前一个例子一样,这条语句使用SELECT语句从表products 中选择数据。在这个例子中,指定了3个列名,列名之间用逗号分隔。分析:

3.查找所有列

输入:

SELECT *
FROM products;

输出:

prod_idvend_idprod_nameprod_priceprod_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
DTNTR 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
TNT1 1003 TNT (1 stick) 2.5 TNT, red, single stick
TNT2 1003 TNT (5 sticks) 10 TNT, red, pack of 10 sticks

分析:

如果给定一个通配符(*),则返回表中所有列。列的顺序一般是列在表定义中出现的顺序。但有时候并不是这样的,表的模式的变化(如添加或删除列)可能会导致顺序的变化。

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
SLING
TNT1
TNT2

分析:

此条语句在所要查找的列名前面加上了表的名字,进行了限定,查找的结果为products表中的prod_name列。

SQL语句--查找数据select

标签:included   log   uitable   name   com   mysql   cas   ati   mit   

原文地址:https://www.cnblogs.com/zhoulixiangblog/p/12078669.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!