简体   繁体   English

MYSQL:条件语句中的条件

[英]MYSQL:Where condition in Case statement

I have table room_category_charges with fields like 我有带有以下字段的表room_category_charges

id room_category(id) category_charges payment_type(id)
1       1                   300              1
2       1                   600              2
3       2                   400              1
4       2                   800              2

and there is another table patient_detail(patient_admission) where tpa(third_party_name) is selected. 还有另一个表Patient_detail(Patient_admission),其中选择了tpa(third_party_name)。 Whether or not tpa name is selected the rates of payment type varies. 不论是否选择了tpa名称,付款方式的费率都不同。

Accordingly I am trying to use the following mysql query using case. 因此,我正在尝试使用以下MySQL查询用例。

I want to return the charges 我想退还费用

  1. as 600 where room_category is 1 and tpa_name is not null. 为600,其中room_category为1,tpa_name不为null。
  2. as 400 where room_category is 2 and tpa_name is null 为400,其中room_category为2,tpa_name为null

SQL: SQL:

  select rn.room_name, 
    CASE WHEN p.tpa_name is NULL
    THEN rcc.category_charges where rcc.payment_type=1
    ELSE rcc.category_charges where rcc.payment_type=2
    END AS 'charges'
  from estimate e,patient_detail p,room_name n

the where clause in then and else statement is generating the error, how I can incorporate the where clause in the case statement. then和else语句中的where子句会产生错误,我如何将case语句中的where子句合并。

Thanks. 谢谢。

Ok I found one solution, which seems to be working fine. 好的,我找到了一种解决方案,看来效果很好。 Any better solution is welcome. 任何更好的解决方案都是值得欢迎的。

SELECT rn.room_name, rcc.category_charges
FROM estimate e,room_category rc, room_name rn, room_category_charges rcc, patient_detail p
WHERE rn.room_category = rc.id and rcc.room_category=rc.id
AND e.alloted_bed = p.bed_type
AND rcc.payment_type =2 and e.alloted_bed=rn.id and p.tpa_name is not null

union

SELECT rn.room_name, rcc.category_charges
FROM estimate e,room_category rc, room_name rn, room_category_charges rcc, patient_detail p
WHERE rn.room_category = rc.id and rcc.room_category=rc.id
AND e.alloted_bed = p.bed_type
AND rcc.payment_type =1 and e.alloted_bed=rn.id and p.tpa_name is null

where is a clause of the select statement and you cannot use it in a case operator. select语句的子句where ,您不能在case运算符中使用它。

The syntax of case can be as follows: case的语法可以如下:

 CASE value WHEN [compare_value] THEN result [WHEN [compare_value] THEN result ...] [ELSE result] END CASE WHEN [condition] THEN result [WHEN [condition] THEN result ...] [ELSE result] END 

As I understand the question, you might want a query like this 据我了解的问题,您可能需要这样的查询

select rn.room_name, 
rcc.category_charges
from estimate e
join room_name rn on e.alloted_bed=rn.id
join patient_detail p on e.ipd_patient_id=p.ipd_patient_id
join room_category_charges rcc on rn.room_category=rcc.id and ((rcc.payment_type=1 and p.tpa_name is null) or (rcc.payment_type=2 and p.tpa_name is not null))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM