[英]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 我在这里输入图像描述,我从6个表(即凭证,采购,采购,供应商,市场,农民)中获取数据,我将凭证表中的ID与采购和采购表进行匹配并获取数据,我的问题是在采购表i中,当我以农民的身份获取源我想从农民表以及卖方和市场表中获取所有数据
I tried using inner join with switch case and inner query but i am getting error 我尝试将内部联接与开关盒和内部查询一起使用,但出现错误
SQL Query SQL查询
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. 我希望它能起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.