SQL Tutorial and Tricks

本文最后更新于 1 分钟前,文中所描述的信息可能已发生改变。

COUNT Function

COUNT(*), COUNT(1), COUNT(column_name)

  • COUNT(*): 统计所有行,不管是否为 NULL
  • COUNT(1): 统计所有代码行,不管是否为 NULL
  • COUNT(column_name): 统计指定列的行数,判断是否为 NULL

Speed

  • COUNT(*)COUNT(1) 的速度是一样的,因为 COUNT(*) 会统计所有列,而 COUNT(1) 会统计所有代码行
  • COUNT(*) 是SQL92定义的标准统计数的语法,所以速度会快一些
  • COUNT(column_name) 会判断是否为 NULL,所以速度会慢一些

USING Function

The USING clause is used to specify some condition to join tables.

sql
SELECT * FROM table1 JOIN table2 USING (column_name);
-- equal to
SELECT * FROM table1 JOIN table2 ON table1.column_name = table2.column_name;

ORDER BY Clause

ORDER BY 1

order by the first column in the select list.

sql
SELECT * FROM table_name ORDER BY 1;
SELECT first_column, second_column FROM table_name ORDER BY 1;

JOIN Clause

JOIN Cheat Sheet

SQL JOIN

Reference

E-commerce projects development issues
Initial Type and Package Type