简体   繁体   中英

How to fetch record from multiple table in MySQL

I normalize my MySQL database which makes me unable to fetch accurate data, actually I need to get detail information about vendors. I have 5 tables. First table is "vendor" in which I have vendor basic info like name, email, address like shown below:

|     id     |     name         |      email        |   address   |
|------------|------------------|----------------------------------
|     1      |    Haris         |  Haris@gmail.com  | Abcd Efgh   |
|------------|------------------|----------------------------------
|     2      |    John          |  john@gmail.com   | Abcd Efgh   |
|------------|------------------|----------------------------------
|     3      |    Chris         | chris@gmail.com   | Abcd Efgh   |

Second table "materials" I have a list of materials in which I have two column id and material like shown below:

|     id     |     materials    |     
|------------|------------------|
|     1      |      Iron        |  
|------------|------------------|
|     2      |    Plastic       |  
|------------|------------------|
|     3      |     Steel        | 

Third Table is "categories" in which I have a list of categories id and categories like shown below:

|     id     |    categories    |     
|------------|------------------|
|     1      |      chair       |  
|------------|------------------|
|     2      |      Table       |  
|------------|------------------|
|     3      |      Glass       | 

In the fourth table which named "vendors_materials" I have vendor id, vendor name and material code.

|     id     |     name         |    material_cod   |   
|------------|------------------|--------------------
|     1      |    Haris         |           1       | 
|------------|------------------|--------------------
|     1      |    Haris         |           2       | 
|------------|------------------|--------------------
|     3      |    Chris         |           1       | 

and In the last fifth table which named "vendors_category" I have vendor id, vendor name and category code.

|     id     |     name         |    category_cod   |   
|------------|------------------|--------------------
|     2      |    John          |           1       | 
|------------|------------------|--------------------
|     2      |    John          |           2       | 
|------------|------------------|--------------------
|     1      |    Haris         |           1       | 

I want to get detail of vendor along with materials and categories like shown below:

|  id |  name    |      email        |   address   | material    | category    |
|-----|----------|-------------------|-----------------------------------------
|  1  |  Haris   |  Haris@gmail.com  | Abcd Efgh   | Iron plastic|  Chair
|-----|------------------|-----------------------------------------------------|
|  2  |  John    |  john@gmail.com   | Abcd Efgh   |             | Chair table
|-----|------------------|-----------------------------------------------------|
|  3  |  Chris   | chris@gmail.com   | Abcd Efgh   |   Iron      |             |

I have done lot of attempts to achieve my results but failed.

Try following query:

Actually you need to learn use of JOIN in SQL. If you learn this thing then you will be great to make hard queries.

For reference you can go on : SQL Joins

SELECT vendor.name,
       vendor.email,
       vendor.address,
       materials.materials,
       categories.categories
FROM vendor
JOIN materials
JOIN categories
JOIN vendors_materials
JOIN vendors_category ON materials.id = materials.material_cod
AND categories.id = vendors_category.material
AND materials.id = vendor.id
AND vendor.id = categories.id
GROUP BY name;
SELECT 
vendor.name,
vendor.email,
vendor.address,
materials.materials AS material,
categories.categories AS category
FROM vendor 
INNER JOIN vendors_materials ON vendor.name = vendors_category.name
INNER JOIN categories ON vendors_category.category_cod = categories.id
INNER JOIN vendors_materials ON vendors_materials.name = vendor.name
INNER JOIN materials ON materials.id = vendors_materials.material_code

also it is not well normalized database structure, first of all it is not recommended to connect tables with varchar columns when you can to connect with ints. For example your 'connector' tables vendors_materials and vendors_category is connected to vendor by name when you can connect by vendor.id . So your vendors_materials table should look like:

|     id     |     vendor_id    |    material_cod   |   
|------------|------------------|--------------------
|     1      |    1             |           1       | 
|------------|------------------|--------------------
|     1      |    1             |           2       | 
|------------|------------------|--------------------
|     3      |    3             |           1       | 

you can do the same in case of vendors_category .

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