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.
Related Posts:
- MySQL Error: : ‘Access denied for user ‘root’@’localhost’
- MySQL Error: : ‘Access denied for user ‘root’@’localhost’
- How do I use properly CASE..WHEN in MySQL
- Error Code: 2013. Lost connection to MySQL server during query
- how to drop partition without dropping data in MySQL?
- MySQL equivalent of DECODE function in Oracle
- MySQL Error: : ‘Access denied for user ‘root’@’localhost’
- What is the difference between “INNER JOIN” and “OUTER JOIN”?
- What is the difference between “INNER JOIN” and “OUTER JOIN”?
- Case in Select Statement
- Unknown column in ‘field list’ error on MySQL Update query
- Unknown column in ‘field list’ error on MySQL Update query
- Self Join to get employee manager name
- MySQL – Operand should contain 1 column(s)
- MySQL “CREATE TABLE IF NOT EXISTS” -> Error 1050
- MySQL create table if not exists and insert record only if table was created
- How can I do a FULL OUTER JOIN in MySQL?
- MySQL query String contains
- What’s the difference between VARCHAR and CHAR?
- MySQL Cannot Add Foreign Key Constraint
- How do I restore a dump file from mysqldump?
- How to split the name string in mysql?
- How do I import an SQL file using the command line in MySQL?
- mysql Foreign key constraint is incorrectly formed error
- MySQL: Invalid use of group function
- Compare dates in MySQL
- MySQL “WITH” clause
- What is the difference between JOIN and UNION?
- MySQL Multiple Joins in one query?
- “CASE” statement within “WHERE” clause in SQL Server 2008
- Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2)
- 1052: Column ‘id’ in field list is ambiguous
- 1052: Column ‘id’ in field list is ambiguous
- SQL Switch/Case in ‘where’ clause
- difference between primary key and unique key
- MySQL combine two columns into one column
- MySQL Error 1264: out of range value for column
- Remove duplicate rows in MySQL
- How to SUM and SUBTRACT using SQL?
- The backend version is not supported to design database diagrams or tables
- Create mysql table directly from CSV file using the CSV Storage engine?
- Simulate CREATE DATABASE IF NOT EXISTS for PostgreSQL?
- Cast from VARCHAR to INT – MySQL
- SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax — PHP — PDO [duplicate]
- How do you force mysql LIKE to be case sensitive?
- Postgres Error: More than one row returned by a subquery used as an expression
- ERROR 1148: The used command is not allowed with this MySQL version
- Insert into a MySQL table or update if exists
- How to round down to nearest integer in MySQL?
- How do I reset a sequence in Oracle?
- MySQL starts with searching issue
- Is it possible to GROUP BY multiple columns using MySQL?
- MySQL error: key specification without a key length
- Best way to test if a row exists in a MySQL table
- Check if MySQL table exists without using “select from” syntax?
- mysql update column with value from another table
- The used SELECT statements have a different number of columns
- How to delete from multiple tables in MySQL?
- MySQL: Can’t create table (errno: 150)
- How to copy a row and insert in same table with a autoincrement field in MySQL?
- Join vs. sub-query
- Alternative to except in MySQL
- Equivalent of explode() to work with strings in MySQL
- How to do a batch insert in MySQL
- Solutions for INSERT OR UPDATE on SQL Server
- MySQL OPTIMIZE all tables?
- MySQL INSERT INTO table VALUES.. vs INSERT INTO table SET
- MySQL – Get row number on select
- MySQL error: Unknown column in ‘where clause’
- How to truncate the text returned for a column in a MySQL query
- “select * into table” Will it work for inserting data into existing table
- Using union and order by clause in mysql
- Error: Duplicate entry ‘0’ for key ‘PRIMARY’
- Inserting Post Meta From SQL
- How to Join two tables from separate databases within WordPress
- How to execute mulitple statement SQL queries using $wpdb->query?
- Size article thumbnails and retrieve them directly from database
- wpdb_prepare with multiple or condition
- How do I find which articles are missing a featured image in The WordPress database?
- SQL Error wp_commentmeta – incorrect table definition
- WordPress and MySQL: how to transfer Meta_key and Meta_Value from one post_id to another
- How do I get the posts within a certain year/date(with sql query)?
- Database Tables Lost Indexes? How to Recover?
- Create table from array with prepare
- Migrating to WordPress Recipe Plugin and Some SQL Issues
- Using AND and bracket grouping in SQL not working
- Why does changing Database Table Prefixes result in loss of content?
- SQL query to set posts in bulk based on the post content
- WP MultiSite: Can’t select database error after changing to new domain
- $wpdb->get_var – What value is returned when zero records are found?
- DB prefix not updating
- Performing CRUD operations on front end in wordpress
- I wanted to Know that how can I give ingore to 0000-00-00 in my config file
- How to create index (sql) to a meta_key?
- The function is deprecated; use mysql_real_escape_string() instead
- The MySQL Connection could not be established., before it was joomla but i have change it into worpress
- can’t delete a row from post_meta table
- Migrated to Namecheap, now Trouble with Database & wp-config [closed]
- export individual posts to text files or a single csv file
- SQL query : Select a thumbnail for a media