美文网首页MySql学习笔记
MySql之SQL语句学习笔记(一)

MySql之SQL语句学习笔记(一)

作者: Android那些事儿 | 来源:发表于2017-07-05 19:35 被阅读0次

文章摘要:
1、SELECT
2、DISTINCT 语句
3、WHERE 子句
4、引号的使用
5、AND 和 OR 运算符
6、ORDER BY
7、INSERT INTO 语句
8、UPDATE 语句
9、DELETE 语句
10、TOP 子句
11、LIKE 操作符
12、SQL 通配符
13、IN 操作符
14、BETWEEN
15、SQL Alias(别名)


SQL语句对大小写不敏感。SELECT = select。

1、SELECT语句

  • 语法:
SELECT 列名称 FROM 表名称
SELECT * FROM 表名称
  • 案例:
mysql> SELECT * FROM test.Persons;
+------+----------+-----------+----------------+----------+
| Id_P | LastName | FirstName | Address        | City     |
+------+----------+-----------+----------------+----------+
|    1 | Aaa      | John      | Oxford Street  | London   |
|    2 | Bush     | George    | Fifth Avenue   | New York |
|    3 | Carter   | Thomas    | Changan Street | Beijing  |
|    4 | Carter   | George    | xford Street   | London   |
+------+----------+-----------+----------------+----------+
4 rows in set (0.00 sec)

2、DISTINCT 语句

关键词 DISTINCT 用于返回唯一不同的值。

  • 语法:
SELECT DISTINCT 列名称 FROM 表名称
  • 案例:
mysql> select DISTINCT(LastName) from test.Persons;
+----------+
| LastName |
+----------+
| Aaa      |
| Bush     |
| Carter   |
+----------+
3 rows in set (0.00 sec)

3、WHERE 子句

如需有条件地从表中选取数据,可将 WHERE 子句添加到 SELECT 语句。

  • 语法
SELECT 列名称 FROM 表名称 WHERE 列 运算符 值

下面的运算符可在 WHERE 子句中使用:

操作符 描述
= 等于
<> 不等于
> 大于
< 小于
>= 大于等于
<= 小于等于
BETWEEN 在某个范围内
LIKE 搜索某种模式

注释:在某些版本的 SQL 中,操作符 <> 可以写为 !=。

  • 案例
mysql> SELECT * FROM Persons WHERE City='Beijing';
+------+----------+-----------+----------------+---------+
| Id_P | LastName | FirstName | Address        | City    |
+------+----------+-----------+----------------+---------+
|    3 | Carter   | Thomas    | Changan Street | Beijing |
+------+----------+-----------+----------------+---------+
1 row in set (0.00 sec)

4、引号的使用

SQL 使用单引号来环绕文本值(大部分数据库系统也接受双引号)。如果是数值,请不要使用引号。

  • 文本值:
这是正确的:
SELECT * FROM Persons WHERE FirstName='Bush'

这是错误的:
SELECT * FROM Persons WHERE FirstName=Bush
  • 数值:
这是正确的:
SELECT * FROM Persons WHERE Year>1965

这是错误的:
SELECT * FROM Persons WHERE Year>'1965'

5、AND 和 OR 运算符

AND 和 OR 可在 WHERE 子语句中把两个或多个条件结合起来。
如果第一个条件和第二个条件都成立,则 AND 运算符显示一条记录。
如果第一个条件和第二个条件中只要有一个成立,则 OR 运算符显示一条记录。

  • 语法
SELECT 列名称 FROM 表名称 WHERE (列 运算符 值) AND/OR (列 运算符 值)
  • AND案例
mysql> SELECT * FROM Persons WHERE FirstName='Thomas' AND LastName='Carter';
+------+----------+-----------+----------------+---------+
| Id_P | LastName | FirstName | Address        | City    |
+------+----------+-----------+----------------+---------+
|    3 | Carter   | Thomas    | Changan Street | Beijing |
+------+----------+-----------+----------------+---------+
1 row in set (0.00 sec)
  • OR案例
mysql> SELECT * FROM Persons WHERE firstname='Thomas' OR lastname='Carter';
+------+----------+-----------+----------------+---------+
| Id_P | LastName | FirstName | Address        | City    |
+------+----------+-----------+----------------+---------+
|    3 | Carter   | Thomas    | Changan Street | Beijing |
|    4 | Carter   | George    | xford Street   | London  |
+------+----------+-----------+----------------+---------+
2 rows in set (0.00 sec)
  • 组合使用案例
mysql> SELECT * FROM Persons 
    -> WHERE (FirstName='Thomas' OR FirstName='William')
    -> AND LastName='Carter';
+------+----------+-----------+----------------+---------+
| Id_P | LastName | FirstName | Address        | City    |
+------+----------+-----------+----------------+---------+
|    3 | Carter   | Thomas    | Changan Street | Beijing |
+------+----------+-----------+----------------+---------+
1 row in set (0.00 sec)

6、ORDER BY

  • 用于对结果集进行排序。

  • 用于根据指定的列对结果集进行排序。

  • 默认按照升序对记录进行排序。
    如果您希望按照降序对记录进行排序,可以使用 DESC 关键字。

  • 语法

SELECT 列名称 FROM 表名称 ORDER BY 列 DESC/ASC,列 DESC/ASC ...
  • Company降序 案例
+---------+-------------+
| Company | OrderNumber |
+---------+-------------+
| Xiaomi  | 7890        |
| Tencent | 6953        |
| LeEco   | 2876        |
| JD      | 3847        |
| IBM     | 3532        |
| Baidu   | 2356        |
| Apple   | 4698        |
| Ali     | 6534        |
+---------+-------------+
8 rows in set (0.00 sec)
  • Company降序、OrderNumber升序 案例
mysql> SELECT Company, OrderNumber FROM Orders ORDER BY Company DESC, OrderNumber ASC;
+---------+-------------+
| Company | OrderNumber |
+---------+-------------+
| Xiaomi  | 7890        |
| Tencent | 6953        |
| LeEco   | 2876        |
| JD      | 3847        |
| IBM     | 3532        |
| Baidu   | 2356        |
| Apple   | 4698        |
| Ali     | 6534        |
+---------+-------------+
8 rows in set (0.00 sec)

7、INSERT INTO 语句

用于向表格中插入新的行。

  • 语法
INSERT INTO 表名称 VALUES (值1, 值2,....)

我们也可以指定所要插入数据的列:

INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值2,....)
  • 案例
INSERT INTO Persons VALUES (6,'Gates', 'Bill', 'Xuanwumen 10', 'Beijing');
INSERT INTO Persons (LastName, Address) VALUES ('Wilson', 'Champs-Elysees');

mysql> select * from Persons;
+------+----------+-----------+----------------+----------+
| Id_P | LastName | FirstName | Address        | City     |
+------+----------+-----------+----------------+----------+
|    1 | Aaa      | John      | Oxford Street  | London   |
|    2 | Bush     | George    | Fifth Avenue   | New York |
|    3 | Carter   | Thomas    | Changan Street | Beijing  |
|    4 | Carter   | George    | xford Street   | London   |
| NULL | Wilson   | NULL      | Champs-Elysees | NULL     |
|    6 | Gates    | Bill      | Xuanwumen 10   | Beijing  |
+------+----------+-----------+----------------+----------+
6 rows in set (0.01 sec)

8、UPDATE 语句

用于修改表中的数据。

  • 语法:
UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值
  • 案例
mysql> UPDATE Persons SET FirstName = 'Fred',Id_p = 9 
WHERE LastName = 'Wilson';

mysql> select * from Persons;
+------+----------+-----------+----------------+----------+
| Id_P | LastName | FirstName | Address        | City     |
+------+----------+-----------+----------------+----------+
|    1 | Aaa      | John      | Oxford Street  | London   |
|    2 | Bush     | George    | Fifth Avenue   | New York |
|    3 | Carter   | Thomas    | Changan Street | Beijing  |
|    4 | Carter   | George    | xford Street   | London   |
|    9 | Wilson   | Fred      | Champs-Elysees | NULL     |
|    6 | Gates    | Bill      | Xuanwumen 10   | Beijing  |
+------+----------+-----------+----------------+----------+
6 rows in set (0.00 sec)

9、DELETE 语句

用于删除表中的行。

  • 语法
DELETE FROM 表名称 WHERE 列名称 = 值

删除所有行,可以在不删除表的情况下删除所有的行。这意味着表的结构、属性和索引都是完整的:

DELETE FROM table_name
或者:
DELETE * FROM table_name

10、TOP 子句

用于规定要返回的记录的数目。
对于拥有数千条记录的大型表来说,TOP 子句是非常有用的。
注释:并非所有的数据库系统都支持 TOP 子句。
SQL Server 的语法:

SELECT TOP number|percent column_name(s)
FROM table_name

MySQL 语法:

SELECT column_name(s)
FROM table_name
LIMIT number

Oracle 语法:

SELECT column_name(s)
FROM table_name
WHERE ROWNUM <= number

11、LIKE 操作符

用于在 WHERE 子句中搜索列中的指定模式。
SQL LIKE 操作符语法:

SELECT column_name(s)
FROM table_name
WHERE column_name LIKE/NOT LIKE pattern
  • 案例
mysql> SELECT * FROM Persons
    -> WHERE City NOT LIKE '%lon%';
+------+----------+-----------+----------------+----------+
| Id_P | LastName | FirstName | Address        | City     |
+------+----------+-----------+----------------+----------+
|    2 | Bush     | George    | Fifth Avenue   | New York |
|    3 | Carter   | Thomas    | Changan Street | Beijing  |
|    6 | Gates    | Bill      | Xuanwumen 10   | Beijing  |
+------+----------+-----------+----------------+----------+
3 rows in set (0.01 sec)

mysql> SELECT * FROM Persons WHERE City LIKE '%lon%';
+------+----------+-----------+---------------+--------+
| Id_P | LastName | FirstName | Address       | City   |
+------+----------+-----------+---------------+--------+
|    1 | Aaa      | John      | Oxford Street | London |
|    4 | Carter   | George    | xford Street  | London |
+------+----------+-----------+---------------+--------+
2 rows in set (0.00 sec

12、SQL 通配符

在搜索数据库中的数据时,SQL 通配符可以替代一个或多个字符。
SQL 通配符必须与 LIKE 运算符一起使用。
在 SQL 中,可使用以下通配符:

通配符 描述
% 替代一个或多个字符
_ 仅替代一个字符
[charlist] 字符列中的任何单一字符
[^charlist]或者[!charlist] 不在字符列中的任何单一字符
  • 案例

MySql对于[]操作略显不同,需要使用REGEXP 替代LIKE。

mysql> SELECT * FROM Persons WHERE City REGEXP '[BO]';
+------+----------+-----------+----------------+----------+
| Id_P | LastName | FirstName | Address        | City     |
+------+----------+-----------+----------------+----------+
|    1 | Aaa      | John      | Oxford Street  | London   |
|    2 | Bush     | George    | Fifth Avenue   | New York |
|    3 | Carter   | Thomas    | Changan Street | Beijing  |
|    4 | Carter   | George    | xford Street   | London   |
|    6 | Gates    | Bill      | Xuanwumen 10   | Beijing  |
+------+----------+-----------+----------------+----------+
5 rows in set (0.00 sec)

mysql> SELECT * FROM Persons WHERE City REGEXP '^[BO]';
+------+----------+-----------+----------------+---------+
| Id_P | LastName | FirstName | Address        | City    |
+------+----------+-----------+----------------+---------+
|    3 | Carter   | Thomas    | Changan Street | Beijing |
|    6 | Gates    | Bill      | Xuanwumen 10   | Beijing |
+------+----------+-----------+----------------+---------+
2 rows in set (0.00 sec)

mysql> 

详情请参阅:MySql基础(一)

13、IN 操作符

IN 操作符允许我们在 WHERE 子句中规定多个值。

  • 语法
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...)
  • 案例
mysql> SELECT * FROM Persons
    -> WHERE LastName IN ('Adams','Carter');
+------+----------+-----------+----------------+---------+
| Id_P | LastName | FirstName | Address        | City    |
+------+----------+-----------+----------------+---------+
|    3 | Carter   | Thomas    | Changan Street | Beijing |
|    4 | Carter   | George    | xford Street   | London  |
+------+----------+-----------+----------------+---------+
2 rows in set (0.01 sec)

14、BETWEEN

BETWEEN 操作符在 WHERE 子句中使用,作用是选取介于两个值之间的数据范围。

操作符 BETWEEN ... AND 会选取介于两个值之间的数据范围。这些值可以是数值、文本或者日期。

  • 语法:
SELECT column_name(s)
FROM table_name
WHERE column_name
BETWEEN value1 AND value2

如需使用上面的例子显示范围之外的人,请使用 NOT 操作符

不同的数据库对 BETWEEN...AND 操作符的处理方式是有差异的。某些数据库会列出介于 "value1" 和 "value2" 之间的人,但不包括 "value1" 和 "value2" ;某些数据库会列出介于 "value1" 和 "value2" 之间并包括 "value1" 和 "value2" 的人;而另一些数据库会列出介于 "value1" 和 "value2" 之间的人,包括 "value1" ,但不包括 "value2" 。
所以,请检查你的数据库是如何处理 BETWEEN....AND 操作符的!

  • 案例:
SELECT * FROM Persons
WHERE LastName
NOT BETWEEN 'Adams' AND 'Carter'

mysql> SELECT * FROM Persons
    -> WHERE LastName
    -> NOT BETWEEN 'Adams' AND 'Carter';
+------+----------+-----------+----------------+---------+
| Id_P | LastName | FirstName | Address        | City    |
+------+----------+-----------+----------------+---------+
|    1 | Aaa      | John      | Oxford Street  | London  |
|    9 | Wilson   | Fred      | Champs-Elysees | NULL    |
|    6 | Gates    | Bill      | Xuanwumen 10   | Beijing |
+------+----------+-----------+----------------+---------+
3 rows in set (0.00 sec)

15、SQL Alias(别名)

通过使用 SQL,可以为列名称和表名称指定别名(Alias)。让SQL语句更易于阅读。

  • Alias 语法
SELECT column_name(s)FROM table_nameAS alias_name
列的 SQL Alias 语法
SELECT column_name AS alias_nameFROM table_name
  • 案例:
mysql> SELECT p.LastName AS Family, p.FirstName AS Name
    -> FROM Persons as p;
+--------+--------+
| Family | Name   |
+--------+--------+
| Aaa    | John   |
| Bush   | George |
| Carter | Thomas |
| Carter | George |
| Wilson | Fred   |
| Gates  | Bill   |
+--------+--------+
6 rows in set (0.00 sec)

相关文章

网友评论

    本文标题:MySql之SQL语句学习笔记(一)

    本文链接:https://www.haomeiwen.com/subject/lppqhxtx.html