La requête de table unique fait référence à l'interrogation des données requises à partir d'une table de données.
(1) Interroger tous les champs
(2) Interroger les champs spécifiés
(3) Interroger les enregistrements spécifiés
(4) Interroger avec le mot-clé in
(5) Plage avec entre et Interroger
(6) Requête de correspondance de caractères avec like
(7) Requête de valeur nulle
(8) Requête multi-conditions avec et
(9) Requête multi-conditions avec ou
(10) Requête les résultats ne sont pas répétés
(11) Trier les résultats de la requête
(12) Requête de groupe
(13) Utiliser la limite pour limiter le nombre de résultats de la requête
(Gratuit recommandation d'apprentissage : tutoriel vidéo mysql)
1. >select * from fruits
2.select f_id,s_id,f_name,f_price from fruits;
mysql> select * from fruits;+------+------+------------+---------+| f_id | s_id | f_name | f_price |+------+------+------------+---------+| 12 | 104 | lemon | 6.40 || a1 | 101 | apple | 5.20 || a2 | 103 | apricot | 2.20 || b1 | 101 | blackberry | 10.20 || b2 | 104 | berry | 7.60 || b5 | 107 | xxxx | 3.60 || bs1 | 102 | orange | 11.20 || bs2 | 105 | melon | 8.20 || c0 | 101 | cherry | 3.20 || m1 | 106 | mango | 15.70 || m2 | 105 | xbabay | 2.60 || m3 | 105 | xxtt | 11.60 || o2 | 103 | coconut | 9.20 || t1 | 102 | banana | 10.30 || t2 | 102 | grape | 5.30 || t4 | 107 | xbabay | 3.60 |+------+------+------------+---------+16 rows in set (0.00 sec)
select 列名 from 表名;
mysql> select f_name from fruits;+------------+| f_name |+------------+| lemon || apple || apricot || blackberry || berry || xxxx || orange || melon || cherry || mango || xbabay || xxtt || coconut || banana || grape || xbabay |+------------+16 rows in set (0.00 sec)
select 字段名1,字段名2,...字段名n from 表名;
[Exemple] Obtenez deux colonnes nommées f_name et f_price à partir de la table fruits. L'instruction SQL est la suivante :
mysql> select f_name,f_price from fruits;+------------+---------+| f_name | f_price |+------------+---------+| lemon | 6.40 || apple | 5.20 || apricot | 2.20 || blackberry | 10.20 || berry | 7.60 || xxxx | 3.60 || orange | 11.20 || melon | 8.20 || cherry | 3.20 || mango | 15.70 || xbabay | 2.60 || xxtt | 11.60 || coconut | 9.20 || banana | 10.30 || grape | 5.30 || xbabay | 3.60 |+------------+---------+16 rows in set (0.00 sec)
select 字段名1,字段名2,。。。,字段名n from 表名 where 查询条件
mysql> select f_name,f_price -> from fruits -> where f_price = 10.2;+------------+---------+| f_name | f_price |+------------+---------+| blackberry | 10.20 |+------------+---------+1 row in set (0.00 sec)
mysql> select f_name,f_price -> from fruits -> where f_name = 'apple';+--------+---------+| f_name | f_price |+--------+---------+| apple | 5.20 |+--------+---------+1 row in set (0.00 sec)
mysql> select f_name,f_price -> from fruits -> where f_price<10.00;+---------+---------+| f_name | f_price |+---------+---------+| lemon | 6.40 || apple | 5.20 || apricot | 2.20 || berry | 7.60 || xxxx | 3.60 || melon | 8.20 || cherry | 3.20 || xbabay | 2.60 || coconut | 9.20 || grape | 5.30 || xbabay | 3.60 |+---------+---------+11 rows in set (0.00 sec)
mysql> select s_id,f_name,f_price -> from fruits -> where s_id in(101,102) -> order by f_name;+------+------------+---------+| s_id | f_name | f_price |+------+------------+---------+| 101 | apple | 5.20 || 102 | banana | 10.30 || 101 | blackberry | 10.20 || 101 | cherry | 3.20 || 102 | grape | 5.30 || 102 | orange | 11.20 |+------+------------+---------+6 rows in set (0.00 sec)
mysql> select s_id,f_name,f_price -> from fruits -> where s_id not in (101,102) -> order by f_name;+------+---------+---------+| s_id | f_name | f_price |+------+---------+---------+| 103 | apricot | 2.20 || 104 | berry | 7.60 || 103 | coconut | 9.20 || 104 | lemon | 6.40 || 106 | mango | 15.70 || 105 | melon | 8.20 || 105 | xbabay | 2.60 || 107 | xbabay | 3.60 || 105 | xxtt | 11.60 || 107 | xxxx | 3.60 |+------+---------+---------+10 rows in set (0.00 sec)
mysql> select f_name,f_price from fruits where f_price between 2.00 and 10.20;+------------+---------+| f_name | f_price |+------------+---------+| lemon | 6.40 || apple | 5.20 || apricot | 2.20 || blackberry | 10.20 || berry | 7.60 || xxxx | 3.60 || melon | 8.20 || cherry | 3.20 || xbabay | 2.60 || coconut | 9.20 || grape | 5.30 || xbabay | 3.60 |+------------+---------+12 rows in set (0.00 sec)
mysql> select f_name,f_price -> from fruits -> where f_price not between 2.00 and 10.20;+--------+---------+| f_name | f_price |+--------+---------+| orange | 11.20 || mango | 15.70 || xxtt | 11.60 || banana | 10.30 |+--------+---------+4 rows in set (0.00 sec)
[Exemple 1] Rechercher tous les fruits commençant par la lettre "b", l'instruction SQL est la suivante :
mysql> select f_id,f_name -> from fruits -> where f_name like 'b%';+------+------------+| f_id | f_name |+------+------------+| b1 | blackberry || b2 | berry || t1 | banana |+------+------------+3 rows in set (0.00 sec)
mysql> select f_id,f_name -> from fruits -> where f_name like '%g%';+------+--------+| f_id | f_name |+------+--------+| bs1 | orange || m1 | mango || t2 | grape |+------+--------+3 rows in set (0.00 sec)
mysql> select f_id,f_name -> from fruits -> where f_name like 'b%y';+------+------------+| f_id | f_name |+------+------------+| b1 | blackberry || b2 | berry |+------+------------+2 rows in set (0.00 sec)
[Exemple] Dans la table des fruits, la requête se termine par la lettre 'y', et il n'y a que 4 lettres devant 'y'. L'instruction SQL est la suivante :
mysql> select f_id,f_name -> from fruits -> where f_name like '____y';+------+--------+| f_id | f_name |+------+--------+| b2 | berry |+------+--------+1 row in set (0.00 sec)
create table customers( c_id int not null auto_increment, c_name char(50) not null, c_address char(50) null, c_city char(50) null, c_zip char(10) null, c_contact char(50) null, c_email char(255) null, primary key (c_id) );
mysql> insert into customers(c_id,c_name,c_address,c_city,c_zip,c_contact,c_email) -> values -> (10001,'redhool','200 Street ','Tianjin','300000','LiMing','LMing@163.com'), -> (10002,'Stars','333 Fromage Lane','Dalian','116000','Zhangbo','Jerry@hotmail.com'), -> (10003,'Netbhood','1 Sunny Place','Qingdao','266000','LuoCong',null), -> (10004,'JOTO','829 Riverside Drive','Haikou','570000','YangShan','sam@hotmail.com');Query OK, 4 rows affected (0.06 sec)Records: 4 Duplicates: 0 Warnings: 0
mysql> select c_id,c_name,c_email from customers where c_email is null;+-------+----------+---------+| c_id | c_name | c_email |+-------+----------+---------+| 10003 | Netbhood | NULL |+-------+----------+---------+1 row in set (0.00 sec)
mysql> select c_id,c_name,c_email -> from customers -> where c_email is not null;+-------+---------+-------------------+| c_id | c_name | c_email |+-------+---------+-------------------+| 10001 | redhool | LMing@163.com || 10002 | Stars | Jerry@hotmail.com || 10004 | JOTO | sam@hotmail.com |+-------+---------+-------------------+3 rows in set (0.00 sec)
mysql> select f_id,f_price,f_name -> from fruits -> where s_id = '101' and f_price>=5; +------+---------+------------+ | f_id | f_price | f_name | +------+---------+------------+ | a1 | 5.20 | apple | | b1 | 10.20 | blackberry | +------+---------+------------+ 2 rows in set (0.00 sec)
mysql> select f_id ,f_price , f_name -> from fruits -> where s_id in ('101','102') and f_price >=5 and f_name = 'apple';+------+---------+--------+| f_id | f_price | f_name |+------+---------+--------+| a1 | 5.20 | apple |+------+---------+--------+1 row in set (0.05 sec)
mysql> select s_id,f_name,f_price -> from fruits -> where s_id=101 or s_id=102;+------+------------+---------+| s_id | f_name | f_price |+------+------------+---------+| 101 | apple | 5.20 || 101 | blackberry | 10.20 || 102 | orange | 11.20 || 101 | cherry | 3.20 || 102 | banana | 10.30 || 102 | grape | 5.30 |+------+------------+---------+6 rows in set (0.00 sec)
mysql> select s_id,f_name,f_price -> from fruits -> where s_id in(101,102);+------+------------+---------+| s_id | f_name | f_price |+------+------------+---------+| 101 | apple | 5.20 || 101 | blackberry | 10.20 || 102 | orange | 11.20 || 101 | cherry | 3.20 || 102 | banana | 10.30 || 102 | grape | 5.30 |+------+------------+---------+6 rows in set (0.00 sec
Notez que
or peut être utilisé avec et, mais faites attention à la priorité des deux, car et La priorité de est supérieure à ou Les opérandes des deux côtés de. et sont d'abord exploités, puis combinés avec les opérandes de ou.select distinct 字段名 from 表名;
[Exemple] Interrogez le champ s_id dans le valeur de la table fruits, renvoie la valeur du champ s_id et ne doit pas être répétée. L'instruction SQL est la suivante : mysql> select distinct s_id from fruits;+------+| s_id |+------+| 104 || 101 || 103 || 107 || 102 || 105 || 106 |+------+7 rows in set (0.05 sec)
mysql> select f_name from fruits order by f_name;+------------+| f_name |+------------+| apple || apricot || banana || berry || blackberry || cherry || coconut || grape || lemon || mango || melon || orange || xbabay || xbabay || xxtt || xxxx |+------------+16 rows in set (0.00 sec)
order by
2.【例】查询fruits表中的f_name和f_price字段,先按f_name排序,再按f_price排序,SQL语句如下:
mysql> select f_name,f_price -> from fruits -> order by f_name, f_price;+------------+---------+| f_name | f_price |+------------+---------+| apple | 5.20 || apricot | 2.20 || banana | 10.30 || berry | 7.60 || blackberry | 10.20 || cherry | 3.20 || coconut | 9.20 || grape | 5.30 || lemon | 6.40 || mango | 15.70 || melon | 8.20 || orange | 11.20 || xbabay | 2.60 || xbabay | 3.60 || xxtt | 11.60 || xxxx | 3.60 |+------------+---------+16 rows in set (0.00 sec)
3.指定排序方向desc
【例1】查询fruits表中的f_name和f_price字段,对结果按f_price降序方式排序,SQL语句如下:
mysql> select f_name,f_price -> from fruits -> order by f_price desc;+------------+---------+| f_name | f_price |+------------+---------+| mango | 15.70 || xxtt | 11.60 || orange | 11.20 || banana | 10.30 || blackberry | 10.20 || coconut | 9.20 || melon | 8.20 || berry | 7.60 || lemon | 6.40 || grape | 5.30 || apple | 5.20 || xxxx | 3.60 || xbabay | 3.60 || cherry | 3.20 || xbabay | 2.60 || apricot | 2.20 |+------------+---------+16 rows in set (0.00 sec)
【例2】查询fruits表,先按f_price降序排序,再按f_name字段升序排序,SQL语句如下:
mysql> select f_price,f_name -> from fruits -> order by f_price desc,f_name;+---------+------------+| f_price | f_name |+---------+------------+| 15.70 | mango || 11.60 | xxtt || 11.20 | orange || 10.30 | banana || 10.20 | blackberry || 9.20 | coconut || 8.20 | melon || 7.60 | berry || 6.40 | lemon || 5.30 | grape || 5.20 | apple || 3.60 | xbabay || 3.60 | xxxx || 3.20 | cherry || 2.60 | xbabay || 2.20 | apricot |+---------+------------+16 rows in set (0.00 sec)
[group by 字段][having<条件表达式>]
1.创建分组
【例1】根据s_id对fruits表中的数据进行分组,SQL语句如下;
mysql> select s_id,count(*) as total -> from fruits -> group by s_id;+------+-------+| s_id | total |+------+-------+| 104 | 2 || 101 | 3 || 103 | 2 || 107 | 2 || 102 | 3 || 105 | 3 || 106 | 1 |+------+-------+7 rows in set (0.05 sec)
可以看到group by 子句按照s_id排序并对数据分组。
【例2】根据s_id对fruits表中的数据进行分组,将每个供应商的水果名称显示出来,SQL语句如下:
mysql> select s_id,group_concat(f_name) as Names -> from fruits -> group by s_id;+------+-------------------------+| s_id | Names |+------+-------------------------+| 101 | apple,blackberry,cherry || 102 | orange,banana,grape || 103 | apricot,coconut || 104 | lemon,berry || 105 | melon,xbabay,xxtt || 106 | mango || 107 | xxxx,xbabay |+------+-------------------------+7 rows in set (0.05 sec)
2.使用having过滤分组
【例】根据s_id对fruits表中的数据进行分组,并显示水果种类大于1的分组信息,SQL语句如下:
mysql> select s_id,group_concat(f_name) as Names -> from fruits -> group by s_id having count(f_name) >1;+------+-------------------------+| s_id | Names |+------+-------------------------+| 101 | apple,blackberry,cherry || 102 | orange,banana,grape || 103 | apricot,coconut || 104 | lemon,berry || 105 | melon,xbabay,xxtt || 107 | xxxx,xbabay |+------+-------------------------+6 rows in set (0.00 sec)
3.在group by 子句中使用with rollup
【例】根据s_id对fruits表中的数据进行分组,并显示记录数量,SQL语句如下:
mysql> select s_id,count(*) as Total -> from fruits -> group by s_id with rollup;+------+-------+| s_id | Total |+------+-------+| 101 | 3 || 102 | 3 || 103 | 2 || 104 | 2 || 105 | 3 || 106 | 1 || 107 | 2 || NULL | 16 |+------+-------+8 rows in set (0.05 sec)
4.多字段分组
【例】根据s_id和f_name字段对fruits表中的数据进行分组,SQL语句如下:
mysql> select * from fruits group by s_id,f_name;+------+------+------------+---------+| f_id | s_id | f_name | f_price |+------+------+------------+---------+| 12 | 104 | lemon | 6.40 || a1 | 101 | apple | 5.20 || a2 | 103 | apricot | 2.20 || b1 | 101 | blackberry | 10.20 || b2 | 104 | berry | 7.60 || b5 | 107 | xxxx | 3.60 || bs1 | 102 | orange | 11.20 || bs2 | 105 | melon | 8.20 || c0 | 101 | cherry | 3.20 || m1 | 106 | mango | 15.70 || m2 | 105 | xbabay | 2.60 || m3 | 105 | xxtt | 11.60 || o2 | 103 | coconut | 9.20 || t1 | 102 | banana | 10.30 || t2 | 102 | grape | 5.30 || t4 | 107 | xbabay | 3.60 |+------+------+------------+---------+16 rows in set (0.00 sec)
5.group by 和order by一起使用
创建数据表演示:
mysql> create table orderitems -> ( -> o_num int not null, -> o_item int not null, -> f_id char(10) not null, -> quantity int not null, -> item_price decimal(8,2) not null, -> primary key (o_num,o_item) -> );Query OK, 0 rows affected (0.13 sec)mysql> insert into orderitems(o_num,o_item,f_id,quantity,item_price) -> values(30001,1,'a1',10,5.2), -> (30001,2,'b2',3,7.6), -> (30001,3,'bs1',5,11.2), -> (30001,4,'bs2',15,9.2), -> (30002,1,'b3',2,20.0), -> (30003,1,'c0',100,10), -> (30004,1,'o2',50,2.50), -> (30005,1,'c0',5,10), -> (30005,2,'b1',10,8.99), -> (30005,3,'a2',10,2.2), -> (30005,4,'m1',5,14.99);Query OK, 11 rows affected (0.06 sec)Records: 11 Duplicates: 0 Warnings: 0
【例】查询订单价格大于100的订单号和总订单价格,SQL语句如下;
mysql> select o_num,sum(quantity * item_price) as orderTotal -> from orderitems -> group by o_num -> having sum(quantity * item_price) >=100;+-------+------------+| o_num | orderTotal |+-------+------------+| 30001 | 268.80 || 30003 | 1000.00 || 30004 | 125.00 || 30005 | 236.85 |+-------+------------+4 rows in set (0.00 sec)
可以看到orderTotal列的总订单价格并没有按照一定的顺序显示,接下来使用order by关键字按总订单价格排列显示结果,SQL语句如下:
mysql> select o_num,sum(quantity * item_price) as orderTotal -> from orderitems -> group by o_num -> having sum(quantity * item_price)>=100 -> order by orderTotal;+-------+------------+| o_num | orderTotal |+-------+------------+| 30004 | 125.00 || 30005 | 236.85 || 30001 | 268.80 || 30003 | 1000.00 |+-------+------------+4 rows in set (0.00 sec)
可以看到,group by 子句按订单号对数据进行分组,sum()函数便可以返回总的订单价格,having子句对分组数据进行过滤,使得只返回总价格大于100的订单,最后使用order by子句排序输出。
select返回所有匹配的行,有可能是表中所有的行,如仅仅需要返回第一行或者前几行,使用limit关键字,基本语法如下:
limit [位置偏移量,] 行数
【例1】显示fruits表查询结果的前4行,SQL语句如下:
mysql> select * from fruits limit 4;+------+------+------------+---------+| f_id | s_id | f_name | f_price |+------+------+------------+---------+| 12 | 104 | lemon | 6.40 || a1 | 101 | apple | 5.20 || a2 | 103 | apricot | 2.20 || b1 | 101 | blackberry | 10.20 |+------+------+------------+---------+4 rows in set (0.00 sec)
【例2】在fruits表中,使用limit子句,返回从第5个记录开始的、行数长度为3的记录,SQL语句如下:
mysql> select * from fruits limit 4,3;+------+------+--------+---------+| f_id | s_id | f_name | f_price |+------+------+--------+---------+| b2 | 104 | berry | 7.60 || b5 | 107 | xxxx | 3.60 || bs1 | 102 | orange | 11.20 |+------+------+--------+---------+3 rows in set (0.00 sec)
也可以使用"limit 4 offset 3
"也是获取从第5条记录开始后面的3条记录。
更多相关免费学习推荐:mysql教程(视频)
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!