技术文章:Mysql查询语句之连表查询和增删改查语句补充
Python进阶学习交流陈侯立身何坦荡,虬须虎眉仍大颡。
查询
连表查询方式一,where连表
SELECT * from 表1,表2 WHERE 表1.外键列=表2.被外键列;SELECT 表1.列1,表1.列2,表2.列1,... from 表1,表2 WHERE 表1.外键列=表2.被外键列;
SELECT * from student,class WHERE student.class_id=class.id;
SELECT student.id,student.`name`,class.title from student,class WHERE student.class_id=class.id;
方式二,left连表
SELECT * from 表1 LEFT JOIN 表2 on 表1.外键字段=表2.被外键字段;SELECT 表1.列1,表1.列2,表2.列1,... from 表1 LEFT JOIN 表2 on 表1.外键字段=表2.被外键字段;
SELECT * from student LEFT JOIN class on student.class_id=class.id;
SELECT student.id,student.`name`,class.title from student LEFT JOIN class on student.class_id=class.id;
方式三,inner连表
其他操作
增(insert)单条插入
INSERT INTO 表(列名1,列名2,...) values(值1,值2,...);
INSERT into student(name,age,gender,class_id) VALUES("吴彦祖",22,"男",1);
多条插入
INSERT INTO 表(列名1,列名2,...) values(值1,值2,...),(值1,值2,...);
INSERT INTO student (NAME, age, gender, class_id)VALUES ("范冰冰", 18, "女", 2), ("成龙", 24, "男", 3);
改(update)
UPDATE <表> set 列 = 值 where <条件>;
UPDATE student set age = 88 where name = "张三"
删除(delete)
delete from <表名>delete from <表名> where <条件>
DELETE from student where name="张三"
总结