How to parse Mysql multi-table queries

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.