Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 766 Vote(s) - 3.56 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Case statement in MySQL

#1
I have a database table called '**tbl_transaction**' with the following definition:

id INT(11) Primary Key
action_type ENUM('Expense', 'Income')
action_heading VARCHAR (255)
action_amount FLOAT

I would like to generate two columns: `Income Amt` and `Expense Amt`.

Is it possible to populate the columns conditionally, using only a SQL Query, such that the output appears in the correct column, depending on whether it is an Expense item or an Income item?

For example:

ID Heading Income Amt Expense Amt
1 ABC 1000 -
2 XYZ - 2000


I'm using MySQL as the database. I'm trying to use the CASE statement to accomplish this.

Cheers!
Reply

#2
Try to use `IF(condition, value1, value2)`

SELECT ID, HEADING,
IF(action_type='Income',action_amount,0) as Income,
IF(action_type='Expense',action_amount,0) as Expense
Reply

#3
This should work:

select
id
,action_heading
,case when action_type='Income' then action_amount else 0 end
,case when action_type='Expense' then expense_amount else 0 end
from tbl_transaction
Reply

#4
MySQL also has `IF()`:

SELECT
id, action_heading,
IF(action_type='Income',action_amount,0) income,
IF(action_type='Expense', action_amount, 0) expense
FROM tbl_transaction
Reply

#5
Yes, something like this:

SELECT
id,
action_heading,
CASE
WHEN action_type = 'Income' THEN action_amount
ELSE NULL
END AS income_amt,
CASE
WHEN action_type = 'Expense' THEN action_amount
ELSE NULL
END AS expense_amt

FROM tbl_transaction;

---

As other answers have pointed out, MySQL also has the `IF()` function to do this using less verbose syntax. I generally try to avoid this because it is a MySQL-specific extension to SQL that isn't generally supported elsewhere. `CASE` is standard SQL and is much more portable across different database engines, and I prefer to write portable queries as much as possible, only using engine-specific extensions when the portable alternative is *considerably* slower or less convenient.
Reply

#6
Another thing to keep in mind is there are two different CASEs with MySQL: one like what @cdhowie and others describe here (and documented here:

[To see links please register here]

) and something which is called a CASE, but has completely different syntax and completely different function, documented here:

[To see links please register here]


Invariably, I first use one when I want the other.
Reply

#7
I hope this would provide you with the right solution:

Syntax:

CASE
WHEN search_condition THEN statement_list
[WHEN search_condition THEN statement_list]....
[ELSE statement_list]
END CASE

Implementation:


select id, action_heading,
case when
action_type="Expense" then action_amount
else NULL
end as Expense_amt,
case when
action_type ="Income" then action_amount
else NULL
end as Income_amt
from tbl_transaction;


Here I am using `CASE` statement as it is more flexible than `if-then-else`. It allows more than one branch. And `CASE` statement is standard SQL and works in most databases.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through