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;  

结果:

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

以上就是Mysql多表查询的实现方法。

 

 

【编辑推荐】

MySQL CONVERT函数简介

带您了解mysql变量

撤权并删除MySQL用户的方法

教您如何实现MySQL动态视图

MySQL查询乱码的解决方法

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

2010-11-22 15:56:34

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多表联合查询

2022-04-01 11:14:48

MySQLJava索引

2012-07-06 09:00:34

MySQL

2022-03-04 12:09:25

SQL数据量多表查询

2010-11-22 16:05:53

MySQL多表插入

2010-10-14 14:33:15

MySQL多表联查

2009-09-15 10:35:11

linq多表查询

2009-09-22 15:26:30

Hibernate多表

2010-10-28 16:42:04

oracle多表查询

2010-06-02 18:07:44

MySQL数据库

2009-06-18 13:58:06

Hibernate多表Hibernate

2010-04-12 17:47:01

Oracle多表查询

2010-05-07 11:00:25

Oracle多表查询

2009-09-17 18:05:15

linq to sql

2010-11-25 14:52:35

MySQL随机查询

2023-11-14 09:08:12

MySQL多表关联
点赞
收藏

51CTO技术栈公众号