Analysis of the implementation of MYSQL Multi-table Query _mysql

Source: Internet
Author: User
Query is the core of the database, the following for you to introduce MySQL multiple table query How to achieve, if you are in the MySQL multiple table query has encountered problems, may wish to see.
mysql multiple table query:
Copy Code code as follows:

CREATE TABLE IF not EXISTS contacts (
contact_id Int (one) 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 (one) not NULL auto_increment,
Droit varchar (255),
PRIMARY KEY (droit_id)
);
CREATE TABLE IF not EXISTS contactdroit (
contactdroit_id Int (one) not NULL auto_increment,
contact_id Int (11),
droit_id Int (11),
PRIMARY KEY (contactdroit_id)
);
Insert into Contacts (contact_id, user_name) VALUES (1, ' User1 ');
Insert into Contacts (contact_id, user_name) VALUES (2, ' user2 ');
Insert into Contacts (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 contacts C, Contactdroit CD, Droit d
where c.contact_id = cd.contact_id
and cd.droit_id = d.droit_id;

Results:
Copy Code code as follows:

contact_id droit_id Droit
1 1 Admin
2 1 Admin
3 2 Superuser

The above is the MySQL multiple table query implementation method.
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.