Reputation: 41
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 | [email protected] | 9000000000 |
| 102 | asus | [email protected] | 8000000000 |
| 103 | hp | [email protected] | 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 | [email protected] |
| 1002 | 101 | Sam Greenblatt | [email protected] |
| 1003 | 102 | Jerry Shen | [email protected] |
| 1004 | 103 | Dion J. Weisler | [email protected] |
+-------------------+--------------+-----------------+------------------------+
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 ?
Upvotes: 0
Views: 59
Reputation: 470
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;
Upvotes: 1