Mysql多表查询的实现

数据库 MySQL
查询是Mysql数据库的核心功能,下文就教您如何实现Mysql多表查询,如果您对此方面感兴趣的话,不妨一看。

Mysql多表查询是大家经常会遇到的问题,下面就为您详细介绍Mysql多表查询的实现方法,希望可以让您对Mysql多表查询有更多的了解。

多表查询:

  1. CREATE TABLE IF NOT EXISTS contact(  
  2. contact_id int(11) NOT NULL AUTO_INCREMENT,  
  3. user_name varchar(255),  
  4. nom varchar(255),  
  5. prenom varchar(255),  
  6. mail varchar(64),  
  7. passcode char(64),  
  8. PRIMARY KEY(contact_id)  
  9. );  
  10. CREATE TABLE IF NOT EXISTS droit(  
  11. droit_id int( 11 ) NOT NULL AUTO_INCREMENT ,  
  12. droit varchar(255),  
  13. PRIMARY KEY(droit_id)  
  14. );  
  15. CREATE TABLE IF NOT EXISTS contactdroit(  
  16. contactdroit_id int(11) NOT NULL AUTO_INCREMENT,  
  17. contact_id int( 11 ),  
  18. droit_id int( 11 ),  
  19. PRIMARY KEY( contactdroit_id )  
  20. );  
  21. Insert into contact(contact_id, user_name) values(1,'user1');  
  22. Insert into contact(contact_id, user_name) values(2,'user2');  
  23. Insert into contact(contact_id, user_name) values(3,'user3');  
  24. Insert into droit(droit_id, droit) values(1,'admin');  
  25. Insert into droit(droit_id, droit) values(2,'superuser');  
  26. Insert into contactdroit(contact_id, droit_id) values(1, 1);  
  27. Insert into contactdroit(contact_id, droit_id) values(2, 1);  
  28. Insert into contactdroit(contact_id, droit_id) values(3, 2);  
  29.  
  30. SELECT c.contact_id, d.droit_id, d.droit FROM contact c, contactdroit cd, droit d   
  31. where c.contact_id = cd.contact_id  
  32. and cd.droit_id = d.droit_id;  
  33.  

结果:

  1. contact_id     droit_id     droit  
  2. 1                      1           admin  
  3. 2                      1           admin  
  4. 3                  2          superuser  

多表联查例子:

两个方法都可以,inner join on 更好点。表结构没贴出来,但比较好懂了。
简单方法:

  1. select c.nom, e.nom   
  2. from consultant c, affaire a, besoin b, salarie sa, site s, entreprise e  
  3. where c.consultant_id=a.consultant_id and a.besoin_id=b.besoin_id and b.salarie_id=sa.salarie_id and ssa.site_id=s.site_id and s.entreprise_id=e.entreprise_id  
  4.  

inner join方法:

  1. select c.nom, e.nom  
  2. from consultant c  
  3. inner join affaire a on c.consultant_id=a.consultant_id  
  4. inner join besoin b on a.besoin_id=b.besoin_id  
  5. inner join salarie sa on b.salarie_id=sa.salarie_id  
  6. inner join site s on ssa.site_id=s.site_id  
  7. inner join entreprise e on s.entreprise_id=e.entreprise_id  
  8.  

 

 

【编辑推荐】

MySQL分区表对于函数的限制

拆表用的MySQL存储过程

深入探讨MySQL锁机制

详解MySQL数据表类型

单表多字段MySQL模糊查询的实现


 

 

责任编辑:段燃 来源: 互联网
相关推荐

2010-10-14 14:28:03

Mysql多表查询

2010-11-22 15:34:17

MySQL多表更新

2010-11-23 14:40:04

MySQL多表删除

2010-10-15 15:02:37

Mysql多表删除

2010-11-23 11:44:10

MySQL多表联合查询

2012-07-06 09:00:34

MySQL

2022-04-01 11:14:48

MySQLJava索引

2022-03-04 12:09:25

SQL数据量多表查询

2010-10-14 14:33:15

MySQL多表联查

2010-11-22 16:05:53

MySQL多表插入

2009-09-22 15:26:30

Hibernate多表

2010-10-28 16:42:04

oracle多表查询

2009-09-15 10:35:11

linq多表查询

2010-04-12 17:47:01

Oracle多表查询

2010-05-07 11:00:25

Oracle多表查询

2009-09-17 18:05:15

linq to sql

2010-06-02 18:07:44

MySQL数据库

2009-06-18 13:58:06

Hibernate多表Hibernate

2010-11-25 14:52:35

MySQL随机查询

2010-10-21 11:10:57

SQL Server查
点赞
收藏

51CTO技术栈公众号