Query is the core of the database. The following describes how to implement Mysql multi-Table query. If you have encountered any problems in Mysql multi-Table query, take a look.
Mysql multi-Table query:
Copy codeThe Code is as follows:
Create table if not exists contact (
Contact_id int (11) not null AUTO_INCREMENT,
User_name varchar (255 ),
Nom varchar (255 ),
Prenom varchar (255 ),
Mail varchar (64 ),
Passcode char (64 ),
Primary key (contact_id)
);
Create table if not exists droit (
Droit_id int (11) not null AUTO_INCREMENT,
Droit varchar (255 ),
Primary key (droit_id)
);
Create table if not exists contactdroit (
Contactdroit_id int (11) not null AUTO_INCREMENT,
Contact_id int (11 ),
Droit_id int (11 ),
Primary key (contactdroit_id)
);
Insert into contact (contact_id, user_name) values (1, 'user1 ');
Insert into contact (contact_id, user_name) values (2, 'user2 ');
Insert into contact (contact_id, user_name) values (3, 'user3 ');
Insert into droit (droit_id, droit) values (1, 'admin ');
Insert into droit (droit_id, droit) values (2, 'superuser ');
Insert into contactdroit (contact_id, droit_id) values (1, 1 );
Insert into contactdroit (contact_id, droit_id) values (2, 1 );
Insert into contactdroit (contact_id, droit_id) values (3, 2 );
SELECT c. contact_id, d. droit_id, d. droit FROM contact c, contactdroit cd, droit d
Where c. contact_id = cd. contact_id
And cd. droit_id = d. droit_id;
Result:
Copy codeThe Code is as follows:
Contact_id droit_id droit
1 1 admin
2 1 admin
3 2 superuser
The above is the implementation of Mysql multi-Table query.