Mysql multi-Table query implementation

Source: Internet
Author: User

Mysql multi-Table query is a common problem. The following describes how to implement Mysql multi-Table query, we hope you can learn more about Mysql multi-table queries.

Multi-Table query:

 
 
  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;  
  33.  

Result:

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

Example of Multi-table join query:

Both methods can be used, and inner join on is better. The table structure is not pasted out, but it is easy to understand.
Simple Method:

 
 
  1. select c.nom, e.nom   
  2. from consultant c, affaire a, besoin b, salarie sa, site s, entreprise e  
  3. where c.consultant_id=a.consultant_id and a.besoin_id=b.besoin_id and b.salarie_id=sa.salarie_id and ssa.site_id=s.site_id and s.entreprise_id=e.entreprise_id  
  4.  

Inner join method:

 
 
  1. select c.nom, e.nom  
  2. from consultant c  
  3. inner join affaire a on c.consultant_id=a.consultant_id  
  4. inner join besoin b on a.besoin_id=b.besoin_id  
  5. inner join salarie sa on b.salarie_id=sa.salarie_id  
  6. inner join site s on ssa.site_id=s.site_id  
  7. inner join entreprise e on s.entreprise_id=e.entreprise_id  
  8.  

Restrictions on Functions of MySQL partition tables

MySQL Stored Procedure for table splitting

In-depth discussion on MySQL Lock Mechanism

Detailed description of MySQL Data Table types

Implementation of MySQL fuzzy query with multiple fields in a single table


 

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.