zoukankan      html  css  js  c++  java
  • Python 学习笔记:Intermediate SQL (1)

    这里主要是整理 SQL 一些进阶的语法,方便自己不记得的时候可以快速查找。

    String Patterns


    /* LIKE */
    SELECT column1, column2, ... FROM table1 WHERE column1 LIKE '%pattern%';
    
    /* BETWEEN ... AND */
    SELECT column1, column2, ... FROM table1 WHERE (column1 BETWEEN condition1 AND condition2);
    

    Sorting


    /* in ascending order */
    SELECT column1, column2, ... FROM table1 ORDER BY column1;
    
    /* in descending order */
    SELECT column1, column2, ... FROM table1 ORDER BY column1 DESC, column2 DESC;
    
    /* multiple tables */
    SELECT t1.column1, t2.column2, ... FROM table1 as t1, table2 as t2 ORDER BY t1.column1, t2.column2 DESC;
    

    Grouping


    /* general syntax */
    SELECT * FROM table1 GROUP BY column1;
    
    /* includes SQL aggregate functions */
    SELECT column1, COUNT(*) as "Number", AVG(column2) as "Average" FROM table1 GROUP BY column1;
    
    /* limit to the groups that count is greater than n */
    SELECT column1, COUNT(*) as "Number", AVG(column2) as "Average" FROM table1 GROUP BY column1 HAVING count(*) > n
    

    Aggregate Functions


    /* get the total value */
    SELECT SUM(column1) FROM table1;
    
    /* get the average value */
    SELECT AVG(column1) FROM table1;
    
    /* get the maximum value */
    SELECT MAX(column1) as "Max" FROM table1;
    

    Scalar and String Functions


    /* display the rounded value of a column */
    SELECT ROUND(column1) FROM table1;
    
    /* display the length of every row of a column */
    SELECT LENGTH(column1) FROM table1;
    
    /* select the row that its uppercase value of column1 is equal to "STR" */
    SELECT * FROM table1 WHERE UCASE(column1) == "STR";
    

    Date and Time Functions


    /* display the day of monthe */
    SELECT DAYAY(column_date) FROM table1;
    
    /* displays row that the date is on the 6th month */
    SELECT * FROM table1 WHERE MONTH(column_date) == '06';
    
    /* displays the third day from the date */
    SELECT (column_date + 3 DAYSDAYS) FROM table1;
    
    /* displays the day between today's date and the date */
    SELECT (CURRENT DATE - column_date) FROM table1;
    
    作者:Yuki
    本文版权归作者和博客园所有,欢迎转载,转载请标明出处(附上博客链接)。 如果您觉得本篇博文对您有所收获,请点击右下角的 [推荐],谢谢!

    关注我的公众号,不定期更新学习心得
  • 相关阅读:
    Linux压缩和解压缩命令(linux必学基础)
    Windows Server 2012 忘记登录密码怎么办?
    linux双网卡双网关设置
    date命令的帮助信息,使用date命令输出数字做为命名标题则不会有重复标题
    linux中history加入显示时间命令代码
    the MTS failed last time时的解决办法
    linux tar.gz zip 解压缩 压缩命令
    如果centos7添加新网卡,系统不识别的解决办法
    Linux常用命令英文全称与中文解释 (pwd、su、df、du等)
    使用动态规划算法解释硬币找零问题
  • 原文地址:https://www.cnblogs.com/yukiwu/p/15661909.html
Copyright © 2011-2022 走看看