[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','leo@qq.com',41,1); insert into info (name, email,age,depart_id) values ('Kevin','kevin@qq.com',45,2); insert into info (name, email,age,depart_id) values ('Winston','winston@qq.com',27,2); insert into info (name, email,age,depart_id) values ('Kaiser','kaiser@qq.com',40,3); insert into info (name, email,age,depart_id) values ('Scott','scott@qq.com',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 | winston@qq.com | 27 | 2 | +----+---------+----------------+-----+-----------+