Parsing the implementation of Mysql multi table queries
- 2020-05-24 06:21:54
- OfStack
Query is the core of the database, the following is to introduce you how to achieve Mysql multi-table query, if you have encountered problems in Mysql multi-table query, might as well 1 look.
Mysql multi-table query:
Results:
That is how Mysql multi-table queries are implemented.
Mysql multi-table query:
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;
Results:
contact_id droit_id droit
1 1 admin
2 1 admin
3 2 superuser
That is how Mysql multi-table queries are implemented.