简体   繁体   中英

Get values from multiple tables matching the records with if condition in mysql

enter image description here I am fetching data from 6 tables namely voucher,sourcing,procurement,vendor,market,farmer i am matching the id present in voucher table with sourcing and procurement table and retrieving data, my problem is in procurement table i when i get the source as farmer i want to get all the data from farmer table and with vendor and market tables also

I tried using inner join with switch case and inner query but i am getting error

SQL Query

SELECT voucher.voucher_id, sourcing.source_proc_id, procurement.procure_id,
case procurement.procure_from
    when 'farmer' then 
               (select procurement.procure_source, farmer.* from   procurement inner join farmer on 
procurement.procure_source =  farmer.farmer_id)
    ELSE 'No Data' END AS Result
  FROM vendor,market,farmer,
       ((voucher inner join sourcing on voucher.voucher_source_id =               sourcing.source_proc_id) inner join procurement on voucher.voucher_proc_id= procurement.procure_id) WHERE voucher.voucher_status = "ACTIVE" group by voucher.voucher_id

farmer table

 farmer_id | farmer_name | farmer_address
  -----------------------------------------
  F_001     | Name        | Address

vendor table

  vendor_id | vendor_name 
  -----------------------
  V_001     | Vendor

voucher table

 voucher_id | voucher_source_id | voucher_proc_id
  ------------------------------------------------
  VOUC_001   | SRC_001           | PROC_001

Procurement Table

procure_id | procure_from | procure_source
  -------------------------------------------
  PROC_001   | farmer       | F_001
  PROC_002   | vendor       | V_001
  Expected output voucher_id | voucher_source_id | procure_id | procure_from | procure_source | farmer_name ----------------------------------------------------------------------------------------- VOUC_001 | SRC_001 | PROC_001 | farmer | F_001 | Name 

Please tell what is the error you are getting.

In the meanwhile you may try the following. It is very hard for me to test whether it will work or not. So please tell the outcome and the previous error.

SELECT 
    voucher.voucher_id,
    sourcing.source_proc_id,
    procurement.procure_id,
    CASE
        WHEN
            procurement.procure_from = 'farmer'
        THEN
            (SELECT 
                    procurement.procure_source, farmer.*
                FROM
                    procurement
                        INNER JOIN
                    farmer ON procurement.procure_source = farmer.farmer_id)
        ELSE 'No Data'
    END AS Result
FROM
    vendor,
    market,
    farmer,
    ((voucher
    INNER JOIN sourcing ON voucher.voucher_source_id = sourcing.source_proc_id)
    INNER JOIN procurement ON voucher.voucher_proc_id = procurement.procure_id)
WHERE
    voucher.voucher_status = 'ACTIVE'
GROUP BY voucher.voucher_id;

I hope it works.

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