欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SQL学习部分笔记

程序员文章站 2022-05-10 11:03:19
...

SQL学习:

选取出唯一的结果的语法: DISTINCT语法会直接删除重复的行;

SELECT DISTINCT column, another_column, …
FROM mytable
WHERE condition(s);

ORDER BY排序的语法: 来让结果按一个或多个属性列做排序.
结果排序(ordered results)

SELECT column, another_column, …
FROM mytable
WHERE condition(s)
ORDER BY column ASC/DESC;

在查询条件中处理 NULL:

SELECT column, another_column, …
FROM mytable
WHERE column IS/IS NOT NULL
AND/OR another_condition
AND/OR …;

  1. SELECT查询语法:
SELECT column, another_column, …
FROM mytable
WHERE condition(s)
ORDER BY column ASC/DESC
LIMIT num_limit OFFSET num_offset;
  1. 用INNER JOIN 连接表的语法:
SELECT column, another_table_column, …
FROM mytable (主表)
INNER JOIN another_table (要连接的表)
ON mytable.id = another_table.id (主键连接,两个相同的连成1)
WHERE condition(s)
ORDER BY column, … ASC/DESC
LIMIT num_limit OFFSET num_offset
  1. 左连接LEFT JOIN、右连接RIGHT JOIN 和 全连接FULL JOIN:
SELECT column, another_column, …
FROM mytable
INNER/LEFT/RIGHT/FULL JOIN another_table
    ON mytable.id = another_table.matching_id
WHERE condition(s)
ORDER BY column, … ASC/DESC
LIMIT num_limit OFFSET num_offset;

注释:
在表A 连接 B,
LEFT JOIN保留A的所有行,不管有没有能匹配上B ;
RIGHT JOIN则保留所有B里的行;
FULL JOIN 不管有没有匹配上,同时保留A和B里的所有行。