1. 程式人生 > 其它 >[MySQL & Python] 07. 必備的SQL命令

[MySQL & Python] 07. 必備的SQL命令

資料準備

本節用到兩張資料表,他們使用depart_id進行關聯。

depart表
+----+--------+
| id | title  |
+----+--------+
|  1 | 開發   |
|  2 | 運維   |
|  3 | 銷售   |
+----+--------+

info表
+----+---------+----------------+-----+-----------+
| id | name    | email          | age | depart_id |
+----+---------+----------------+-----+-----------+
|  1 | Leo     | leo@qq.com     |  41 |         1 |
|  2 | Kevin   | kevin@qq.com   |  45 |         2 |
|  3 | Winston | winston@qq.com |  27 |         2 |
|  4 | Kaiser  | kaiser@qq.com  |  40 |         3 |
|  5 | Scott   | scott@qq.com   |  40 |         3 |
+----+---------+----------------+-----+-----------+

 

複製一下語句到MySQL客戶端進行資料準備

--建立depart表,並新增內容
create table depart (
  id int not null primary key auto_increment,
  title varchar(20)) default charset=utf8 ;

insert into depart (title) values ('開發'),('運維'),('銷售');

--建立info表,並新增內容
create table info (
  id int not null primary key auto_increment,
  name varchar(20) not null,
  email varchar(40) not null,
  age int not null,
  depart_id int) default charset=utf8;

insert into info (name, email,age,depart_id) values ('Leo','[email protected]',41,1);
insert into info (name, email,age,depart_id) values ('Kevin','[email protected]',45,2);
insert into info (name, email,age,depart_id) values ('Winston','[email protected]',27,2);
insert into info (name, email,age,depart_id) values ('Kaiser','[email protected]',40,3);
insert into info (name, email,age,depart_id) values ('Scott','[email protected]',40,3);

 

條件查詢 where

  • select * from 資料表 where 條件


select * from info where age = 41;
+----+------+------------+-----+-----------+
| id | name | email      | age | depart_id |
+----+------+------------+-----+-----------+
|  1 | Leo  | leo@qq.com |  41 |         1 |
+----+------+------------+-----+-----------+

select * from info where name != 'Leo' or 'Kaiser' or 'Kevin';
+----+---------+----------------+-----+-----------+
| id | name    | email          | age | depart_id |
+----+---------+----------------+-----+-----------+
|  2 | Kevin   | kevin@qq.com   |  45 |         2 |
|  3 | Winston | winston@qq.com |  27 |         2 |
|  4 | Kaiser  | kaiser@qq.com  |  40 |         3 |
|  5 | Scott   | scott@qq.com   |  40 |         3 |
+----+---------+----------------+-----+-----------+

select * from info where age between 41 and 45;
+----+-------+--------------+-----+-----------+
| id | name  | email        | age | depart_id |
+----+-------+--------------+-----+-----------+
|  1 | Leo   | leo@qq.com   |  41 |         1 |
|  2 | Kevin | kevin@qq.com |  45 |         2 |
+----+-------+--------------+-----+-----------+

mysql> select * from info where id in (1,4);
+----+--------+---------------+-----+-----------+
| id | name   | email         | age | depart_id |
+----+--------+---------------+-----+-----------+
|  1 | Leo    | leo@qq.com    |  41 |         1 |
|  4 | Kaiser | kaiser@qq.com |  40 |         3 |
+----+--------+---------------+-----+-----------+

select * from info where id not in (1,4,5);
+----+---------+----------------+-----+-----------+
| id | name    | email          | age | depart_id |
+----+---------+----------------+-----+-----------+
|  2 | Kevin   | kevin@qq.com   |  45 |         2 |
|  3 | Winston