SELECT * FROM tableName;
选择 tableName 中的所有列。
SELECT column1, column2 FROM tableName;
选择 column1, column2 列。
SELECT column1 FROM tableName;
选择 column1 列。
SELECT DISTINCT column1 FROM tableName;
结果去重。
SELECT column1 FROM tableName LIMIT 5;
输出5行。
SELECT column1 FROM tableName LIMIT 4,5;
从行4(第5行)开始输出5行。
SELECT products.prod_name FROM crashcourse.products;
SELECT * FROM tableName ORDER BY column1;
根据 column1 的值排序查询结果。
SELECT * FROM tableName ORDER BY column1, column2;
根据 column1 排序, 相同项再根据 column2 排序。
SELECT * FROM tableName ORDER BY column1 DESC, column2;
column1 降序排序。
SELECT * FROM tableName ORDER BY column1 DESC LIMIT 1;
column1 最大的一条。
SELECT * FROM tableName WHERE column1 = "name1";
限定条件
WHERE
可以使用各种操作符和BETWEEN
。
相等是=
。不等<>
,!=
。
SELECT * FROM tableName WHERE column2 BETWEEN 10 AND 20;
限定范围
SELECT * FROM tableName WHERE column3 IS NULL;
选择空值
SELECT * FROM tableName WHERE column1="name2" AND column2<10;
多个 WHERE 条件组合。
SELECT * FROM tableName WHERE column1 IN ("name1", "name2", "name3");
WHERE 多个条件的一种简写。
SELECT * FROM tableName WHERE column1 NOT IN ("name1", "name2", "name3");
非