常用sql语句-查询

常用sql语句-查询

sql


总览

   
   
  1. SELECT
  2. [ALL | DISTINCT | DISTINCTROW ]
  3. [HIGH_PRIORITY]
  4. [STRAIGHT_JOIN]
  5. [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
  6. [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
  7. select_expr [, select_expr ...]
  8. [FROM table_references
  9. [WHERE where_condition]
  10. [GROUP BY {col_name | expr | position}
  11. [ASC | DESC], ... [WITH ROLLUP]]
  12. [HAVING where_condition]
  13. [ORDER BY {col_name | expr | position}
  14. [ASC | DESC], ...]
  15. [LIMIT {[offset,] row_count | row_count OFFSET offset}]
  16. [PROCEDURE procedure_name(argument_list)]
  17. [INTO OUTFILE 'file_name'
  18. [CHARACTER SET charset_name]
  19. export_options
  20. | INTO DUMPFILE 'file_name'
  21. | INTO var_name [, var_name]]
  22. [FOR UPDATE | LOCK IN SHARE MODE]]

distinct

  • 去除重复行
   
   
  1. select distinct * from tableName;
  • 返回唯一不同字段值(只能作用于一个字段)
   
   
  1. select distinct fieldName from tableName;
  • 统计
   
   
  1. select count(distinct fieldName) from tableName;

as

   
   
  1. select fieldName as newName from tableName;

in

   
   
  1. select * from tableName where fieldName in (value1,value2);

between

   
   
  1. select * from tableName where fieldName between value1 and value2;

is null/not null

   
   
  1. select * from tableName where fieldName is null;

like

  • 单个字符
   
   
  1. select * from tableName where like 'xxx_x';
  • 多个字符
   
   
  1. select * from tableName where like 'xxx%';

limit

   
   
  1. select * from tableName limit 1;
   
   
  1. select * from tableName limit 0,1;

group by

   
   
  1. select field1,count(field2) from tableName group by field1;

order by

   
   
  1. select * from table order by field;

desc/asc

   
   
  1. select * from table order by field desc;

聚合函数

  • AVG() 返回某列的平均值
  • COUNT() 返回某列的行数
  • MAX() 返回某列的最大值
  • MIN() 返回某列的最小值
  • SUM() 返回某列值之和
   
   
  1. select count(*) from table;

having

在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与合计函数一起使用。

   
   
  1. select field1,count(field2) fromt tableName group by field1 having count(field2)<10;

执行顺序

   
   
  1. (8)SELECT (9)DISTINCT (11)<Top Num> <select list>
  2. (1)FROM [left_table]
  3. (3)<join_type> JOIN <right_table>
  4. (2)ON <join_condition>
  5. (4)WHERE <where_condition>
  6. (5)GROUP BY <group_by_list>
  7. (6)WITH <CUBE | RollUP>
  8. (7)HAVING <having_condition>
  9. (10)ORDER BY <order_by_list>
  1. FROM:对FROM子句中的前两个表执行笛卡尔积(Cartesian product)(交叉联接),生成虚拟表VT1
  2. ON:对VT1应用ON筛选器。只有那些使为真的行才被插入VT2。 
    3.OUTER(JOIN):如 果指定了OUTER JOIN(相对于CROSS JOIN 或(INNER JOIN),保留表(preserved table:左外部联接把左表标记为保留表,右外部联接把右表标记为保留表,完全外部联接把两个表都标记为保留表)中未找到匹配的行将作为外部行添加到 VT2,生成VT3.如果FROM子句包含两个以上的表,则对上一个联接生成的结果表和下一个表重复执行步骤1到步骤3,直到处理完所有的表为止。
  3. WHERE:对VT3应用WHERE筛选器。只有使为true的行才被插入VT4.
  4. GROUP BY:按GROUP BY子句中的列列表对VT4中的行分组,生成VT5.
  5. CUBE|ROLLUP:把超组(Suppergroups)插入VT5,生成VT6.
  6. HAVING:对VT6应用HAVING筛选器。只有使为true的组才会被插入VT7.
  7. SELECT:处理SELECT列表,产生VT8.
  8. DISTINCT:将重复的行从VT8中移除,产生VT9.
  9. ORDER BY:将VT9中的行按ORDER BY 子句中的列列表排序,生成游标(VC10).
  10. TOP:从VC10的开始处选择指定数量或比例的行,生成表VT11,并返回调用者。

自连接

   
   
  1. SELECT c1.cust_id, c1.cust_name, c1.cust_contact
  2. FROM Customers AS c1,Customers AS c2
  3. WHERE c1.cust_name = c2.cust_name
  4. AND c2.cust_contact = 'Jim Jones';

自然连接

   
   
  1. SELECT C.*, O.order_num, O.order_date,
  2. OI.prod_id, OI.quantity, OI.item_price
  3. FROM Customers AS C,Orders AS O,OrderItems AS OI
  4. WHERE C.cust_id = O.cust_id
  5. AND OI.order_num = O.order_num
  6. AND prod_id = 'RGAN01';

外连接

   
   
  1. select * from a inner join b on a.id=b.id
   
   
  1. select a.*,b.* from a left join b on a.id=b.id;
   
   
  1. select a.*,b.* from a right join b on a.id=b.id;

子查询

   
   
  1. select * from a where id in (select id from b);

union

   
   
  1. select id from a
  2. union
  3. select id from b;
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值