简体   繁体   中英

Join two tables according to column value specified in parent table

My master table is "ordered_product" table.

I want to join another table according to the value resides in "product_id_type" column inside it.

If my "product_type_id" = 1, I want to join "allopathy_medicine" table. If it is 2 I want to join with "ayurveda_medicine" table.

If the "product_type_id" in "ordered_product" table is 1, means I can only allow to get product name from "allopathy_medicine" table.

The primary objective is to show the product names in a page. 在此处输入图像描述

SELECT ordered_product.*,
    CASE
WHEN product_type_id = 1 THEN allopathy_medicine.name AS allopathyName
WHEN product_type_id = 2 THEN ayurvada - medicine.name AS ayurvedaName
ELSE allopathy_medicine.name
END
FROM ordered_product
INNER JOIN allopathy_medicine ON allopathy_medicine.id = ordered_product.product_id
INNER JOIN ayurvada - medicine ON ayurvada_medicine.id = ordered_product.product_id

The above code is my code, but its not working, it shows syntax error in php my admin. Am lookin forward for answers.

Thank you shinoy

update: I found this code is working from answers, does anyone know how to shrink the code:

SELECT
    op.order_no,
    op.product_type_id,
    op.qty,
    op.amount,
    al.name,
    pt.name AS pdttype
FROM ordered_product op
JOIN allopathy_medicine al ON al.id = op.product_id
JOIN product_type pt ON pt.id = op.product_type_id
WHERE op.product_type_id = 1 AND op.order_no = orderNo //passed externaly
UNION ALL
SELECT
    op.order_no,
    op.product_type_id,
    op.qty,
    op.amount,
    ay.name,
    pt.name AS pdttype
FROM ordered_product op
JOIN `ayurvada-medicine` ay ON ay.id = op.product_id
JOIN product_type pt ON pt.id = op.product_type_id
WHERE op.product_type_id = 2 AND op.order_no = orderNo
UNION ALL
SELECT
    op.order_no,
    op.product_type_id,
    op.qty,
    op.amount,
    ay.name,
    pt.name AS pdttype
FROM ordered_product op
JOIN `veterinary_medicine` ay ON ay.id = op.product_id
JOIN product_type pt ON pt.id = op.product_type_id
WHERE op.product_type_id = 3 AND op.order_no = orderNo
UNION ALL
SELECT
    op.order_no,
    op.product_type_id,
    op.qty,
    op.amount,
    ay.name,
    pt.name AS pdttype
FROM ordered_product op
JOIN `fitness` ay ON ay.id = op.product_id
JOIN product_type pt ON pt.id = op.product_type_id
WHERE op.product_type_id = 4 AND op.order_no = orderNo
UNION ALL
SELECT
    op.order_no,
    op.product_type_id,
    op.qty,
    op.amount,
    ay.name,
    pt.name AS pdttype
FROM ordered_product op
JOIN `cosmetic` ay ON ay.id = op.product_id
JOIN product_type pt ON pt.id = op.product_type_id
WHERE op.product_type_id = 5 AND op.order_no = orderNo
UNION ALL
SELECT
    op.order_no,
    op.product_type_id,
    op.qty,
    op.amount,
    ay.name,
    pt.name AS pdttype
FROM ordered_product op
JOIN `sugical_products` ay ON ay.id = op.product_id
JOIN product_type pt ON pt.id = op.product_type_id
WHERE op.product_type_id = 6 AND op.order_no = orderNo
UNION ALL
SELECT
    op.order_no,
    op.product_type_id,
    op.qty,
    op.amount,
    ay.name,
    pt.name AS pdttype
FROM ordered_product op
JOIN `laboratory_products` ay ON ay.id = op.product_id
JOIN product_type pt ON pt.id = op.product_type_id
WHERE op.product_type_id = 7 AND op.order_no = orderNo
UNION ALL
SELECT
    op.order_no,
    op.product_type_id,
    op.qty,
    op.amount,
    ay.name,
    pt.name AS pdttype
FROM ordered_product op
JOIN `animal_food` ay ON ay.id = op.product_id
JOIN product_type pt ON pt.id = op.product_type_id
WHERE op.product_type_id = 8 AND op.order_no = orderNo

Can't be any more straightforward than this.

SELECT
    op.product_id,
    op.product_type_id,
    IFNULL(al.name, ay.name) AS name
FROM ordered_product op
LEFT JOIN allopathy_medicine al ON al.id = op.product_id AND product_type_id = 1
LEFT JOIN ayurveda_medicine ay ON ay.id = op.product_id AND product_type_id = 2

One product type can be or type 1 or type 2 so you inner join fails ever

If the row don't match always you should use left join

SELECT ordered_product.*,
    CASE
WHEN product_type_id = 1 THEN allopathy_medicine.name 
WHEN product_type_id = 2 THENm2.name 
ELSE allopathy_medicine.name
END  medicine_name
FROM ordered_product
LEFT  JOIN allopathy_medicine ON allopathy_medicine.id = ordered_product.product_id
LEFT  JOIN `ayurvada - medicine` m2 ON m2.id = ordered_product.product_id

and you should not use tablename with minus sign.. you should use underscore..but if your shce contain this char you should wrap the table name with backtics

and you can assign different column name alias inside case when condition but only one common column name alias after end

Assuming that product_id in the main table be unique, and only occurs in at most one of the other two tables, you should be able to just do a series of joins here:

SELECT op.*, COALESCE(alm.name, aym.name) AS medicine_name,
       CASE WHEN aym.id IS NULL THEN 'allopathy' ELSE 'ayurvada' END AS medicine_type
FROM ordered_product op
LEFT JOIN allopathy_medicine alm ON alm.id = op.product_id
LEFT JOIN `ayurvada-medicine` aym ON aym.id = op.product_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