[英]CASE condition in SELECT statement in MySQL
I have CASE condition in SELECT. 我在SELECT中有CASE条件。 Below is the query condition
以下是查询条件
The query is from a view 查询来自视图
SELECT expt_id,stain_type,control_stain, test_stain
CASE WHEN stain_type = 'Blue' THEN control_stain = 'NA'
CASE WHEN stain_type = 'Hemat' THEN test_stain = 'NA'
FROM experiment_results__view
Here the value 'NA' has to be populated for the condition of stain_type. 这里必须为stain_type的条件填充值“NA”。 Would appreciate your help with suggestions on how to write this query
非常感谢您提供有关如何编写此查询的建议
Thanks!! 谢谢!!
I expect you want to use something in the line: 我希望你想在线上使用一些东西:
SELECT
expt_id,
stain_type,
CASE WHEN stain_type = 'Blue' THEN 'NA' ELSE NULL END control_stain,
CASE WHEN stain_type = 'Hemat' THEN 'NA' ELSE NULL END test_stain
FROM experiment_results__view
to get your value of 'NA' into columns named control_stain and test_stain. 将“NA”的值放入名为control_stain和test_stain的列中。 But if you want to override the existing value in those columns with the value 'NA' in just those cases, then use:
但是,如果您想在这些情况下覆盖值为“NA”的那些列中的现有值,请使用:
SELECT
expt_id,
stain_type,
CASE WHEN stain_type = 'Blue' THEN 'NA' ELSE control_stain END control_stain,
CASE WHEN stain_type = 'Hemat' THEN 'NA' ELSE test_stain END test_stain
FROM experiment_results__view
If you're intending an update of your table then you should rewrite this to: 如果您打算更新表格,那么您应该将其重写为:
UPDATE
experiment_results__view
SET
control_stain = CASE WHEN stain_type = 'Blue' THEN 'NA' ELSE control_stain END,
test_stain = CASE WHEN stain_type = 'Hemat' THEN 'NA' ELSE test_stain END
-- if you don't want to update the complete table add an WHERE clause
WHERE
<some condition>
you need to END each case statement 你需要END每个case语句
SELECT expt_id,stain_type,
CASE WHEN stain_type = 'Blue' THEN 'NA' ELSE control_stain END AS 'Control Stain'
CASE WHEN stain_type = 'Hemat' THEN 'NA' ELSE test_stain END AS 'Test Stain'
FROM experiment_results__view
if you are intending to update the table you can do an update query 如果您打算更新表,则可以执行更新查询
UPDATE
experiment_results__view
SET
control_stain = CASE WHEN stain_type = 'Blue' THEN 'NA' ELSE control_stain END,
test_stain = CASE WHEN stain_type = 'Hemat' THEN 'NA' ELSE test_stain END
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.