I have a table with type
field.
how can I check this field in query:
I want to check it if type
== 'a'
then bes
= amount
, bed
= '-'
ELSE bed
= amount
, bes
= '-'
bes and bed is not really exists in my table but amount is (int)
this is my try:
SELECT billing.*,
CASE billing.type WHEN 'A' THEN billing.amount AS bes, '-' AS bed ELSE billing.amount AS bed, '-' AS bes END
FROM billing
... my searches wasn't useful
any solution...
You can create condition for every row, MySQL
supports inline IF
statements.
SELECT billing.*,
IF(billing.type = 'A', billing.amount, '-') AS bes,
IF(billing.type = 'A', '-', billing.amount) AS bed
FROM billing
You'll need two case statements for that:
SELECT billing.*,
CASE billing.type WHEN 'A' THEN billing.amount ELSE '-' AS bes END,
CASE billing.type WHEN 'A' THEN '-' ELSE billing.amount AS bed END
FROM billing
One CASE-WHEN-THEN-ELSE
can have only one return value...
This should do the trick:
SELECT billing.*,
CASE billing.type WHEN 'A' THEN billing.amount ELSE '-' END AS bes,
CASE billing.type WHEN 'A' THEN '-' ELSE billing.amount END AS bed
FROM billing
SELECT billing.*,
CASE WHEN billing.type = 'A' THEN billing.amount ELSE 0 END AS bes,
CASE WHEN billing.type <> 'A' THEN billing.amount ELSE 0 END AS bed
FROM billing
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.