[关闭]
@Chiang 2019-09-18T20:21:26.000000Z 字数 2576 阅读 547

查询数据

MySQL


  1. select
  2. {* | <字段列表>}
  3. [
  4. from <表1>,<表2>...
  5. [
  6. where <表达式>
  7. [group by <group by definition>]
  8. [having <expression> [{<operator> <expression>}...]]
  9. [order by <order by definition>]
  10. [limit [<offset>,] <row count>]
  11. ]
  12. ]
  13. select count(*) sum(`num`) avg(`price`) max(`num`) min(`num`) distinct `name`, `s_id`, group_concat(`name`) as names
  14. from `table_name`
  15. where `id` > 50
  16. and
  17. `id` not in (89,99)
  18. and
  19. `name` like `%jiang--zhan%`
  20. and
  21. `email` is not null
  22. or
  23. `nickname` is null
  24. and
  25. `id` not between 30 and 40
  26. # with rollup 统计计量数量 rollup 和 order by 是互相排斥的
  27. group by `s_id`,`name` [with rollup]
  28. having count(`name`) > 1
  29. order by `id` desc, `name` asc
  30. limit 4,3;

内连接

  1. select suppliers.s_id, s_name, f_name, f_price
  2. from fruits, suppliers
  3. where fruits.s_id = suppliers.s_id;
  4. select suppliers.s_id, s_name, f_name, f_price
  5. from fruits inner join suppliers
  6. on fruits.s_id = suppliers.s_id;
  7. # 自连接查询
  8. select f1.f_id, f1.f_name
  9. from fruits as f1, fruits as f2
  10. where f1.s_id=f2.s_id and f2.f_id='a1';

外连接查询

  1. select customers.c_id, orders.o_num
  2. from customers left outer join orders
  3. on customers.c_id=orders.c_id;
  4. select customers.c_id, orders.o_num
  5. from customers right outer join orders
  6. on customers.c_id=orders.c_id;

复合条件查询

  1. select customers.c_id, orders.o_num
  2. from customers right outer join orders
  3. on customers.c_id=orders.c_id and customers.c_id=1001;
  4. select suppliers.s_id, s_name, f_name, f_price
  5. from fruits inner join suppliers
  6. on fruits.s_id = suppliers.s_id
  7. order by fruits.s_id;

子查询

  1. # any
  2. select num1
  3. from tbl1
  4. where num1 > any (select num2 from tbl2);
  5. # some
  6. select num1
  7. from tbl1
  8. where num1 > some (select num2 from tbl2);
  9. # all
  10. select num1
  11. from tbl1
  12. where num1 > all (select num2 from tbl2);
  13. # exists
  14. select * from fruits
  15. where exists
  16. (select s_name from suppliers where s_id=107);
  17. select * from fruits
  18. where f_price > 10.20 and exists
  19. (select s_name from suppliers where s_id=107);
  20. # exists 和 not exists 的结果只取决于是否会返回行,而不取决于这些行的内容,所以这个子查询输入列表通常是无关紧要的.
  21. select * from fruits
  22. where not exists
  23. (select s_name from suppliers where s_id=107);
  24. # in
  25. select c_id
  26. from orders
  27. where o_num not in
  28. (select o_num from orderitems where f_id='c0');

合并查询结果

  1. # union
  2. select s_id, f_name, f_price
  3. from fruits
  4. where f_price < 9.0
  5. union all
  6. select s_id, f_name, f_price
  7. from fruits
  8. where s_id in(101,103);

正则表达式匹配

  1. # ^ 匹配文本的开始字符
  2. select * from fruits where f_name regexp '^b';
  3. # $ 匹配文本的结束字符
  4. select * from fruits where f_name regexp 'y$';
  5. # . 匹配任何单个字符
  6. select * from fruits where f_name regexp 'a.g';
  7. # * 匹配零个或多个在它前面的字符
  8. select * from fruits where f_name regexp '^ba*';
  9. # + 匹配前面的字符1次或多次
  10. select * from fruits where f_name regexp '^ba+';
  11. # 匹配指定字符串文本
  12. select * from fruits where f_name regexp 'on';
  13. select * from fruits where f_name regexp 'on | ap';
  14. # [] 匹配指定字符串中的任意一个
  15. select * from fruits where f_name regexp [ot];
  16. select * from fruits where f_name regexp [^0-9];
  17. select * from fruits where f_name regexp [0-9a-z];
  18. # 使用{n,}或者{n,m}来指定其前面的字符串连续出现的次数
  19. select * from fruits where f_name regexp 'ba{1,3}';

参考资料:
MySQL5.7从入门到精通

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注