美文网首页
mysql查询语句

mysql查询语句

作者: 暗夜飞鹰_f722 | 来源:发表于2017-09-01 16:42 被阅读0次

本文根据实验楼mysql教程记录,如有侵权请私信我


select查询语句的基本格式是:
select 列名 from 表名 where 限制条件


  1. 数学符号条件限制
    select 列名 from 表名 where id>100;
    
  2. AND和OR
    select 列名 from 表名 where id>100 and age>20;
    select 列名 from 表名 where id>100 or age>20;
    
  3. IN和NOT IN
    select * from 表名 where 列名 in ('value1','value2');
    select * from 表名 where 列名 not in ('value1','value2');
    
  4. 通配符
    _ 表示1个待定字符
    %表示不定个待定字符
    select * from 表名 where 列名 like value_;
    select * from 表名 where 列名 like value%;
    
  5. 排序
    select * from 表名 order by 列名 DESC;      (_降序_)
    select * from 表名 order by 列名 ASC;      (_升序_)
    
    order by也可以用来分类,后面加binary表示区分大小写
    select * from 表名 order by binary 列名 DESC;  
    
  6. SQL内置函数和计算
    select MAX(列名) as 别名 from 表名;
    SELECT owner, COUNT(*) FROM pet GROUP BY owner;
    
    函数名 MIN MAX COUNT SUM AVG
    功能 最小值 最大值 计数 求和 平均值
  7. 子查询
    SELECT of_dpt,COUNT(proj_name) AS count_project FROM project
    WHERE of_dpt IN
    (SELECT in_dpt FROM employee WHERE name='Tom');
    
  8. 连接查询
    查询并返回来自多个表中的信息
    SELECT id,name,people_num
    FROM employee,department
    WHERE employee.in_dpt = department.dpt_name
    ORDER BY id;
    
    核心思想是把多个表组成一个新的表来查询
    SELECT id,name,people_num
    FROM employee JOIN department
    ON employee.in_dpt = department.dpt_name
    ORDER BY id;
    

相关文章

网友评论

      本文标题:mysql查询语句

      本文链接:https://www.haomeiwen.com/subject/eamhjxtx.html