简体   繁体   中英

How to find information from three tables related to each other (MySQL)

I have created a database dealer in MySQL which has all the info about a dealer like :

     dealer id
     dealer name
     dealer email
     dealer phone no
     dealer's primary address
     dealer's billing address
     dealer's contact person
     dealer's contact person's name
     dealer's contact person's email

And this information is stored in three tables namely:

    dealer
    address
    contact_person

The fields and data of tables are given below :

This table dealer has dealer_id as primary key.

    `mysql> select * from dealer;
    +-----------+-------------+----------------+-----------------+
    | dealer_id | dealer_name | dealer_email   | dealer_phone_no |
    +-----------+-------------+----------------+-----------------+
    |       101 | dell        | dell@gmail.com |      9000000000 |
    |       102 | asus        | asus@gmail.com |      8000000000 |
    |       103 | hp          | hp@gmail.com   |      7000000000 |
    +-----------+-------------+----------------+-----------------+
    3 rows in set (0.02 sec)
    `

This table address has field dealer_address_id as primary keyand field add_dealer_id as foreign key which references to the table dealer 's field dealer_id .

    `mysql> select * from address;
    +-------------------+---------------+----------------------+-------------+----------------+
    | dealer_address_id | add_dealer_id | add_line1            |  city       |   address_type |
    +-------------------+---------------+----------------------+-------------+----------------+
    |             10001 |           101 | 1, Dell Avenue       | Round Rock  | Primary        |
    |             10002 |           101 | 1, Dell Avenue       | Round Rock  |  Billing       |
    |             10003 |           102 | 1, Asus Computer     | Fremont     |  Primary       |
    |             10004 |           102 | 1, Asus Headquarters | Taipei      | Billing        |
    |             10005 |           103 | HP Inc               | Palo Alto   |  Primary       |
    |             10006 |           103 | HP Inc               | Bristol     |  Billing       |
    +-------------------+---------------+----------------------+-------------+----------------+
    6 rows in set (0.01 sec)
    `

And this table contact_person has field contact_person_id as primary key and field cp_dealer_id as foreign key which also references to the tables dealer 's field dealer_id .

    `mysql> select * from contact_person;
    +-------------------+--------------+-----------------+------------------------+
    | contact_person_id | cp_dealer_id | con_per_name    | con_per_email          | 
    +-------------------+--------------+-----------------+------------------------+
    |              1001 |          101 | Michael S. Dell | michael.dell@gmail.com |       
    |              1002 |          101 | Sam Greenblatt  | sam.dell@gmail.com     |      
    |              1003 |          102 | Jerry Shen      | jerry.asus@gmail.com   |     
    |              1004 |          103 | Dion J. Weisler | dion.hp@gmail.com      |      
    +-------------------+--------------+-----------------+------------------------+
    4 rows in set (0.01 sec)`

I want to find all the information about a dealer like :

    dealer id
     dealer name
     dealer email
     dealer phone no
     dealer's primary address
     dealer's billing address
     dealer's contact person
     dealer's contact person's name
     dealer's contact person's email

For example i have queries :

    `Find dealer on the basis of dealer id `

    `Find dealer on the basis of dealer name `

    `Find dealer on the basis of dealer email `

So please help me how do I do this ?

What you are looking for is a join statement in MySQL.

Here is an example:

SELECT Dealer.dealer_id, Dealer.Name, Dealer.Email
FROM Dealer
INNER JOIN Address
ON Dealer.dealer_id=Address.dealer_id;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM