• 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
    本文版权归作者和博客园所有,欢迎转载,转载请标明出处(附上博客链接)。 如果您觉得本篇博文对您有所收获,请点击右下角的 [推荐],谢谢!

    关注我的公众号,不定期更新学习心得
  • 相关阅读:
    JS放在head和放在body中的区别
    模板模式(Template Pattern)
    原型模式
    Linux下的头文件搜索路径
    How to Change the Default Theme Appearance [editing with no theme]
    版本控制
    What is libacl.so.1 ?
    交叉编译器 arm-linux-gnueabi 和 arm-linux-gnueabihf 的区别
    mount --bind 的妙用
    mount的bind选项
  • 原文地址:https://www.cnblogs.com/yukiwu/p/15661909.html
Copyright © 2020-2023  润新知