美文网首页
五种连接查询 & pymysql

五种连接查询 & pymysql

作者: 流蓝浅 | 来源:发表于2018-03-21 19:39 被阅读0次
create TABLE emp (
    id int PRIMARY KEY auto_increment,
    name varchar(255) not null,
    age int DEFAULT 18,
    gender varchar(10) DEFAULT '男',
    email varchar(100) UNIQUE,
    deptid int,
    CONSTRAINT fk_emp_dept FOREIGN KEY(deptid)  REFERENCES dept(id)
);

cross jion 交叉查询

尽量的避免使用交叉查询,因为会产生笛卡尔积现象

SELECT *
from emp,dept

第二种写法

select *
from emp cross join dept

内连接

select *
from emp INNER JOIN dept ON(emp.detpid=dept.id)

内连接的变种写法

select *
FROM emp,dept
WHERE emp.detpid = dept.id

外连接

左外连接 右外连接

左外连接

select *
from emp as e LEFT JOIN dept d on(e.detpid=d.id)

右外连接

select e.*,d.name as deptname,d.descs descs
from emp as e right JOIN dept d on(e.detpid=d.id)

自连接

自连接
select * from plate
where pid =
(selec id from plate where name = Java开发)

函数部分

SELECT LENGTH('刘建宏');

SELECT CHAR_LENGTH('刘建宏');

SELECT TRIM(" liujianhong is good man!! ");

SELECT SUBSTRING('liujianhong is good man!! ',4,5);

SELECT ASCII('0');

SELECT ROUND(20.49)

SELECT now();

SELECT curdate();
SELECT curtime();

复习

1、数据库为什么要分表?
我们解决数据冗余
2、多表关联查询
1、cross join
笛卡尔积现象
2、内连接(inner join)
3、外连接(outer join)
outer left join
outer right join
4、自连接()
5、自然连接(nature join)

3、子查询
select deptname
from dept
where deptid = (select detpid from emp where name='lss')

4、外键约束

5、视图
create view v_user_list as select * from user;
select * from v_user_list
6、index索引
7、sql优化
select * from article
id title content desc time modify author

select * from article

相关文章

网友评论

      本文标题:五种连接查询 & pymysql

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