Detailed basic operation of sql query statement

Select query Classification

  • Simple queries
    • Query data from a table
  • Complex queries
    • Multi-table join queries (derived from a plurality of superposed lateral column of the table)
      • En
      • Left connection
      • The right connection
      • Fully connected
      • Since the connection
      • Non-equivalent connections
    • n complex query (query result set vertically superimposed)
      • Union
      • Union All
      • Intersect
      • Minus
    • n subquery
      • Non-correlated subqueries
      • Correlated subquery

Query syntax structure

SELECT select_list  #select_list 要显示的列,列之间用逗号隔开,列也称作投影
[ INTO new_table ]  # 结果集导入新表
FROM [table_source] #表名 
[ WHERE search_condition ] #表行过滤条件
[ GROUP BY group_by_expression ]  #按照指定的列将表行分组
[ HAVING search_condition ] #对分组后的新行进行过滤  
[ ORDER BY order_expression [ ASC | DESC ] ] #按照指定的1个或多个列进行排序,ASC增序,DESC降序

Operators commonly select

  • Arithmetic operators:
    • + Adding
    • - subtraction
    • * Multiplication
    • / DIV or the division
    • % Or MOD remainder operator
  • Logical Operators:
    • and and
    • or or
    • xor XOR
    • or not! non
  • Comparison operators:
    • = Equal
    • ! = Or <> Not equal
    • > Greater than
    • <Less than
    • > = Greater than or equal
    • <= Less than or equal;
    • <=> strict comparison of two NULL values ​​are equal, the same is true NULL
    • is null is null
    • a non-null value is not null;
    • in (value list) value list
    • not in (list of values) is not in the list of values;
    • between high and low values ​​(including low and high values) between the low and high values;
    • not between the high and low values ​​(including low and high values) is not within the high and low value range;
    • like 'Wildcard' according to the wildcard matching;
      • 0% match any number of characters or
      • _ Matches any one character
  • Bitwise Operators
    • & Bitwise AND
    • | Bitwise or
    • ^ Bitwise XOR
    • ! Negate
    • << Left
    • >> Right
  • Assignment Operators
    • : = For assignment select statement,
  • Operator Precedence
priority Operators
1 !
2 ^
3 *, /, DIV,%, MOD
4 -,+
5 <<,>>
6 &
7 |
8 =,<=>,>=,>,<=,<,<>,!=,IS,LIKE,REGEXP,IN
9 BETWEEN,CASE,WHEN,THEN,ELSE
10 NOT
11 &&,AND
12 ||,OR,XOR
13 :=

select where clause

  • stu following table data
    image-20191127143147761
  • 查询学生表中性别为'女',体重超过60公斤的学生的所有信息
    image-20191127142809675
  • 查询学生表中1班或者2班中,身高超过190的学生
    image-20191127142923218
  • 查询学生表中3班学生里面考分在400和520之间的女生
    image-20191127143111204
  • 查询学生表中没有分配班级而且是女生的学生
    image-20191127143249233
  • 在学生表体重低于40公斤且身高低于1.65米的学生,列出其姓名,身高,体重,总分以及总分占750分满分的百分比
    image-20191127143530710
  • 在学生表中查找学生姓名,第二个字是‘侯’,或者 第一个字是‘张’且名字只有两个
    image-20191127143724804

函数

  • 函数用来处理SQL语句中的数据,可以嵌入在SQL语句中使用,增加了SQL语句对数据的处理功能

    • 函数可以有0到多个参数,但是总会有一个返回值
    • 函数可以用来计算、修改、格式化输出二维表中的各类数据
    • 不同数据库的函数的名称和用法略有不同,但都会提供如:字符串处理、数值处理、日期处理、统计等分类的函数、方便用户处理各类数据
  • 常用字符串函数

    • char_length(str) 计算给定字符串长度

      image-20191127144143374

    • concat(str1,str2...) 字符串拼接

      image-20191127144436508

    • substr(str,pos,len) 字符串str从pos位置(1开始)开始截取len个字符

      image-20191127144726414

  • 常用数值函数

    • round(num,n) 数值num对n+1位进行四舍五入运算,正数表示保留的小数位,负数表示对整数位进行四舍五入,0表示四舍五入取整

      image-20191127145058922

  • 常用日期函数

    • year(date) 对日期date取年份

      image-20191127150809181

    • month(date) 对日期date取月份

      image-20191127150922189

    • curdate() 获取当前日期

      image-20191127151014210

    • curtime() 获取当前时间

      image-20191127151045436

    • now() 获取当前时间

      image-20191127151127752

    • datediff(date1,date2) 返回date1与date2之间相隔的天数(date1-date2)

      image-20191127151359574

      也可以这样写

      image-20191127151424890

      image-20191127151439549

  • 常用条件判断函数

    • if(expr,v1,v2) 如果表达式expr成立返回v1,不成立返回v2

      image-20191127151659946

      也可以进行嵌套

      image-20191127151902239

      image-20191127165142041

      select sname,score,if(score>600,'优秀',if(score>500,'良好',if(score>400,'合格','不合格'))) from stu ;
      select sname,substr(sname,if(sname in('大乔','小乔'),2,1 ),if(substr(sname,1,2) in('诸葛','夏侯','太史'),2,1)) 姓氏 from stu;
      #substr(sname,if(sname in('大乔','小乔'),2,1 ),if(substr(sname,1,2) in('诸葛','夏侯','太史'),2,1)) 姓氏 表示使用姓氏作为别名代替原有的列名
  • CASE运算

    • 可以添加多个条件判断,比单个if更加灵活

    • 语法格式:case when expr1 then v1 [when expr2 v2 then] …… else vn end

      image-20191127152600651

      select sname,score,(case when score>600 then '优秀' when score >500 then '良好' when score>400 then '合格' else '不合格' end)level from stu;
  • 常用空值处理函数

    • When null values ​​in the table, any calculation are the function returns null

      image-20191127153039406

    • ifnull (v1, v2) v2 if v1 is null is returned otherwise v1

      image-20191127153308851

      2 shows the placement of students in class and not

    • isnull (expr) returns 1 if expr is empty otherwise it returns 0

      image-20191127153634092

      Show all but placement students

  • Common aggregate functions

    • max (col) returns the largest value, the data type of the column may be numeric, character, date

      image-20191127154218718

    • min () returns the minimum value, the data type of the column may be numeric, character, date

      image-20191127154646401

    • sum () column summation, the data type of the column is not character

      image-20191127154727585

    • AVG () averaging the data type of the column is not character

      image-20191127154748231

    • count () statistics, if available column value is not counted Statistics

      image-20191127154818802

  • DISTINCT keyword

    • For removing duplicates, it needs to be placed before the first field

      image-20191127154953839

  • Aliases

    • Set in place for the display table or column names

    • Column alias syntax: select sname [as] alias from table; as may be omitted

      image-20191127155257432

    • Table alias syntax: select the table alias field names, field names ... from the alias table shows that [as] table aliases;.

      image-20191127155703186

  • order by ordering

    • Query result set specified by the later order by 1 or more columns sort

      • Divided into ascending and descending order
        • ASC ASC, can not write default
        • Descending DESC
      • For the values ​​in ascending order from small to large, in descending order from large to small is
      • For dates and times, from far to near ascending, descending from near to far
      • For English characters from a to z are ascending, descending from a to z
      • For Chinese characters, in ascending order from small to large, the character set encoding descending order according to descending character set encoding, if the selected coding may be sorted gbk phonetic
      • For the name of the sort column, you can use the following ways
        • Column Name
        • Column aliases
        • Column number
        • function
        • expression
    • According to sort from high to low student achievement show

      image-20191127192209472

    • According to students of all ages Sorted by seasoned Young

      image-20191127192635261

    • Alphabetical order by name

      image-20191127192855403

      NOTE: convert (sname using gbk) for converting the character set used

  • limit the display area

    • It used to display part of the query results

    • LIMIT clause last execution

    • Syntax: limit N, M records displayed by the article M N, N starting from 0, the default is omitted, 0 N

      image-20191127160331402

Guess you like

Origin www.cnblogs.com/lastyear/p/11946773.html