查询数据表
语法:
SELECT
{* | <字段列表》}
[
FROM <表1>, <表2>....
[ where <表达式> ]
[ group by <group by definition>] [ having <expr> ]
[ order by <..> ] [ limit <...> ]
示例 :
查询多字段 select f_name,f_price from fruits;
条件半段指定查询 mysql> select f_name, f_price from fruits where f_price = '5.2';
之间查询 mysql> select f_name,f_price from fruits where f_price between 5 and 10;
按数字大小排序 mysql> select distinct s_id from fruits order by s_id,f_name ;
升序desc
降序 asc
统计 相同的 行数 ,以及按s_id 进行分组: mysql> select f_name,s_id,count(*) as ljx from fruits group by s_id;
统计 s_id相同的 f_name
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 | grape,banana,orange |
| 103 | apricot,coconut |
| 104 | lemon,berry |
| 105 | xbabay,xxtt,melon |
| 107 | xxxx,xbababa |
+------+-------------------------+
6 rows in set (0.00 sec)
统计 数字总和
mysql> select s_id ,count(8) 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.00 sec)
mysql> select * from fruits group by s_id,f_name; +------+------+------------+---------+
| f_id | s_id | f_name | f_price |
+------+------+------------+---------+
| a1 | 101 | apple | 5.20 |
| b1 | 101 | blackberry | 10.20 |
| c0 | 101 | cherry | 3.20 |
| t1 | 102 | banana | 10.30 |
| t2 | 102 | grape | 5.30 |
| bs1 | 102 | orange | 11.20 |
| a2 | 103 | apricot | 2.20 |
| o2 | 103 | coconut | 9.20 |
| b2 | 104 | berry | 7.60 |
| l2 | 104 | lemon | 6.40 |
| bs2 | 105 | melon | 8.20 |
| m2 | 105 | xbabay | 2.60 |
| m3 | 105 | xxtt | 11.60 |
| m1 | 106 | mango | 15.60 |
| t4 | 107 | xbababa | 3.60 |
| b5 | 107 | xxxx | 3.60 |
+------+------+------------+---------+
16 rows in set (0.01 sec)
查看前四行 的
mysql> select * from fruits limit 4; +------+------+------------+---------+
| f_id | s_id | f_name | f_price |
+------+------+------------+---------+
| a1 | 101 | apple | 5.20 |
| a2 | 103 | apricot | 2.20 |
| b1 | 101 | blackberry | 10.20 |
| b2 | 104 | berry | 7.60 |
+------+------+------------+---------+
从第四行 开始 往后数 3行
mysql> select * from fruits limit 4,3;
+------+------+--------+---------+
| f_id | s_id | f_name | f_price |
+------+------+--------+---------+
| b5 | 107 | xxxx | 3.60 |
| bs1 | 102 | orange | 11.20 |
| bs2 | 105 | melon | 8.20 |
+------+------+--------+---------+
3 rows in set (0.00 sec)
求所有水果的 单价 总和
sum
集合函数查询
AVG() COUNT()MAX() MIN ()
SUM()
mysql> select avg(f_price) as avg_price from fruits where s_id=103;
+-----------+
| avg_price |
+-----------+
| 5.700000 |
+-----------+
1 row in set (0.00 sec)
求平均值
mysql> select avg(f_price) as avg_price from fruits group by s_id;
+-----------+
| avg_price |
+-----------+
| 6.200000 |
| 8.933333 |
| 5.700000 |
| 7.000000 |
| 7.466667 |
| 15.600000 |
| 3.600000 |
+-----------+
7 rows in set (0.00 sec)
mysql> select s_id,min(f_price) as main_price from fruits group by s_id;
+------+------------+
| s_id | main_price |
+------+------------+
| 101 | 3.20 |
| 102 | 5.30 |
| 103 | 2.20 |
| 104 | 6.40 |
| 105 | 2.60 |
| 106 | 15.60 |
| 107 | 3.60 |
+------+------------+
7 rows in set (0.00 sec)
mysql> select s_id,max(f_price) as max_price from fruits group by s_id;
+------+-----------+
| s_id | max_price |
+------+-----------+
| 101 | 10.20 |
| 102 | 11.20 |
| 103 | 9.20 |
| 104 | 7.60 |
| 105 | 11.60 |
| 106 | 15.60 |
| 107 | 3.60 |
+------+-----------+
7 rows in set (0.00 sec)
mysql> select max(f_name) from fruits;
+-------------+
| max(f_name) |
+-------------+
| xxxx |
+-------------+
1 row in set (0.00 sec)
mysql> select suppliers.s_id ,s_name,f_name,f_price from fruits inner join suppliers on fruits.s_id = suppliers.s_id;
+------+--------+------------+---------+
| s_id | s_name | f_name | f_price |
+------+--------+------------+---------+
| 101 | asdas | apple | 5.20 |
| 101 | asdas | blackberry | 10.20 |
| 102 | sadas | orange | 11.20 |
| 101 | asdas | cherry | 3.20 |
| 102 | sadas | banana | 10.30 |
| 102 | sadas | grape | 5.30 |
+------+--------+------------+---------+
6 rows in set (0.00 sec)
mysql> select * from fruits
-> where exists
-> (SELECT s_name from suppliers where s_id=102);+------+------+------------+---------+
| f_id | s_id | f_name | f_price |
+------+------+------------+---------+
| 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 |
| l2 | 104 | lemon | 6.40 |
| m1 | 106 | mango | 15.60 |
| 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 | xbababa | 3.60 |
+------+------+------------+---------+
16 rows in set (0.00 sec)
mysql> select * from fruits where f_price = (select min(f_price) from fruits); //先执行括号 里的 再执行 括号外面点的
+:匹配前面的字符一次或多次
<字符串>:匹配包含指定的字符串的文本
[字符集合]:匹配字符集合中任何一个字符
[^]:匹配不在括号里的任何字符
字符串{n}:匹配前面的字符串至少N次
字符串{n,m}:匹配前面的字符串至少n次,至多次
mysql> INSERT INTO person_old(id,name,age,info)
-> select id,name,age,info from person;
Query OK, 6 rows affected (0.00 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql> select * from person_old;
+----+---------+-----+----------+
| id | name | age | info |
+----+---------+-----+----------+
| 1 | asd | 21 | teacher |
| 2 | sdf | 22 | dancer |
| 3 | sdfas | 23 | Musician |
| 4 | sdf | 24 | man |
| 9 | oy | 21 | asdasd |
| 10 | tercher | 19 | asdasd |
+----+---------+-----+----------+
6 rows in set (0.00 sec)
UPDATE table_name
set column_name1=values1, column_name2=values2 WHERE .....
mysql> update person set age=15,name='hehe' where id=3;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from person where id=3;
+----+------+-----+----------+
| id | name | age | info |
+----+------+-----+----------+
| 3 | hehe | 15 | Musician |
+----+------+-----+----------+
1 row in set (0.00 sec)
mysql> update person set info='student' where age between 19 and 22;
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4 Changed: 4 Warnings: 0
mysql> select * from person where age between 19 and 22;
+----+---------+-----+---------+
| id | name | age | info |
+----+---------+-----+---------+
| 1 | asd | 21 | student |
| 2 | sdf | 22 | student |
| 9 | oy | 21 | student |
| 10 | tercher | 19 | student |
+----+---------+-----+---------+
4 rows in set (0.00 sec)
mysql> delete from person where id=10;
Query OK, 1 row affected (0.01 sec)
mysql> select * from person where id=10;
Empty set (0.00 sec)
mysql> delete from person where age between 19 and 20;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from person where age between 19 and 20;
Empty set (0.00 sec)
表与表之间的 链接 查看 有相同s_id 的 列表数据
select suppliers.s_id ,s_name ,f_name,f_price from fruits inner join suppliers on fruits.s_id=suppliers.s_id;
供应水果味a1的 人 还 供应了什么
select f1.f_id,f1.f_name from fruits as f1, fruits as f2 where f1.s_id=f2.s_id and f2.f_id='a1';
外链接 用 suppliers 链接 fruits
select fruits.s_id,f_name,s_city from fruits left outer join suppliers on suppliers.s_id=fruits.s_id;
select suppliers.s_id,s_name,f_name,f_price from fruits inner join suppliers on fruits.s_id = suppliers.s_id order by fruits.s_id;
子查询
mysql> select * from fruits where f_price>10 and exists (select * from suppliers where s_id=107); //判断 suppliers 中是否有 s_id=107 如果存在 就输出 fruits 里 f_price 大于 10 块钱的
mysql> select s_id,f_name from fruits where s_id=(select s1.s_id from suppliers as s1 where s1.s_id='101');
使用 union 链接查询 吧 不相关的信息一起显示出来 合并查询结果 :
mysql> select s_id, f_name, f_price from fruits where f_price <10 union all select s_id, f_name,f_price from fruits where s_id in(101,103); //数据类型 和表名是相同的
对比俩个表里面的数据
select num1 from tb1 where num1 > any (select num2 from tb2);
子查询
exists
合并查询结果
查询所有价格小于9块钱的水果 并且查询s_id是 101和103的结果
select column... from table1
union
select column... from table2
查询所有价格小于9块钱的水果 并且查询s_id是 101和103的结果
as
表名 as 表别名
正则表达式的匹配查询
+:匹配前面的字符一次或多次
《字符串》:匹配包含制定的字符串的文本
【字符集和】:匹配自飞机盒中任何一个字符
【^】 :匹配不在括号里的任何字符
字符串{n}:匹配前面的字符串至少n次
字符串{n,m} :匹配前面的字符串至少n次,之多次
*:匹配零个或多个在他前面的字符
$: 匹配文本的结束字符号
. :代表匹配任意一个字符
| 或者
在表中查询以b为开头的水果名字
select * from fruits where f_name regexp '^b';
查询以y为结尾的水果名字
select * from fruits where f_name regexp 'y$';
包含a也包含g并且中间可以匹配任意字符的
select * from fruits where f_name regexp ’a.g’;
所有a和g的都显示出来
select * from fruits where f_name regexp 'a*g';
查看on或者ap的字符
select * from fruits where f_name regexp 'on|ap';
select * from fruits where f_name regexp'x{1,2}';