/ / Jak znaleźć informacje z trzech powiązanych ze sobą tabel (MySQL) - mysql

Jak znaleźć informacje z trzech tabel powiązanych ze sobą (MySQL) - mysql

Stworzyłem bazę danych kupiec w MySQL który zawiera wszystkie informacje o za kupiec lubić :

     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

Te informacje są przechowywane w trzech tabelach, a mianowicie:

    dealer
address
contact_person

Pola i dane tabel podano poniżej:

Ten stół kupiec ma dealer_id jako klucz podstawowy.

    `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)
`

Ten stół adres ma pole dealer_address_id jako klucz podstawowy i pole add_dealer_id jako klucz obcy, który odwołuje się do tabeli kupiecpole 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)
`

I ten stół osoba kontaktowa ma pole contact_person_id jako klucz podstawowy i pole cp_dealer_id jako klucz obcy, który również odwołuje się do tabel kupiecpole 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)`

Chcę znaleźć wszystkie informacje o dystrybutorze, takie jak:

    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

Na przykład mam zapytania:

    `Find dealer on the basis of dealer id `

`Find dealer on the basis of dealer name `

`Find dealer on the basis of dealer email `

Więc proszę, pomóż mi, jak to zrobić?

Odpowiedzi:

1 dla odpowiedzi № 1

To, czego szukasz, to instrukcja dołączenia w MySQL.

Oto przykład:

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