zoukankan      html  css  js  c++  java
  • Mysql常用语句

    SQL常用语句:
    一、常用关键词:
     1.distinct:过滤重复
      select distinct create_user_name from bms_project;  此种情况下,就要用到distinct过滤掉重复的。
      
     2.count: 统计
      select count(*) from bms_project; 查询bms_project表里总共有多少条记录。
      select count(*) from bms_project where project_type=1;  查询bms_project表里project_type=1的总共有多少条记录。
      
     3.top: 取最前面的N条
      select top 3 * from bms_project;  查询bms_project里的最前面3条记录。
      
     4.like: 模糊查询 
      select * from bms_project where project_name like '%项目%' and project_type=2;
      查询bms_project表里project_type=2,并且project_name中包含“项目”二字的记录。
      
     5.between...and...:从...到...; 在...之间
      select * from bms_project where project_budget between 500000 and 1000000;
      查询bms_project表里project_budget在500000到1000000之间的记录。
     
     6.in: 子查询
      select * from bms_project where id in(100,110,120,130,140,150);
      查询bms_project表里id是100,110,120,130,140,150的记录。
      
     7.order by: 排序 (ASC顺序,DESC逆序)
      select * from bms_project order by project_budget DESC;
      
     8.group by: 以...分组。通常和聚合函数配合使用(count(),sum(),avg())
      select project_type,sum(project_budget) from bms_project group by project_type;
      注意:group by 后面的字段和select后非聚合函数的字段一致。
      
     9.limit x,y  (x:起始位置,y:偏移值)
      select project_budget from bms_project order by project_budget desc limit 0,5 ;
      从bms_project表中取出前五条,并按照project_budget由大到小的顺序显示。
      
    二、多表连接查询:
     1. table1 INNER JOIN table2: 内连接。只显示连接的两张表的交集行。
     2. table1 LEFT JOIN table2: 左外连接。显示table1的所有行,即使在table2中没有匹配的行。
     3. table1 RIGHT JOIN table2:右外连接。显示table2的所有行,即使在table1z中没有匹配的行。
     4. table1 FULL JOIN table2: 即使table1中的行在table2中没有匹配,或者table2中的行在table1中没有匹配。它仍返回table1和table2两张表中所有匹配的行。

  • 相关阅读:
    Web 呼起 APP
    移动端监测离开页面
    input 呼起数字键盘
    建站工具Hexo
    linux 查找并kill进程
    linux php --ini
    Git + BeyondCompare
    Linux连续执行多条命令
    chrome start.js报错
    emoji和utf8mb4字符集
  • 原文地址:https://www.cnblogs.com/wangerxiansheng/p/4091716.html
Copyright © 2011-2022 走看看