
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Converting boolean values to positive or negative sign in MySQL?
Following is the syntax −
select if(yourColumnName,1,-1) from yourTableName;
Let us first create a table −
mysql> create table DemoTable ( isMarried boolean ); Query OK, 0 rows affected (0.60 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(true); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values(false); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(false); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(true); Query OK, 1 row affected (0.36 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+ | isMarried | +-----------+ | 1 | | 0 | | 0 | | 1 | +-----------+ 4 rows in set (0.00 sec)
Following is the query to convert boolean to a positive or negative sign in MySQL. Here, we have set negative for boolean false i.e. 0 −
mysql> select if(isMarried,1,-1) from DemoTable;
This will produce the following output −
+--------------------+ | if(isMarried,1,-1) | +--------------------+ | 1 | | -1 | | -1 | | 1 | +--------------------+ 4 rows in set (0.00 sec)
- Related Articles
- BOOLEAN or TINYINT to store values in MySQL?
- Test array values for positive or negative infinity in Numpy
- How to GROUP BY in a select query on positive or negative values?
- How to convert negative values in an R data frame to positive values?
- How to print positive and negative infinity values in JavaScript?
- How to convert positive value to negative while inserting in MySQL?
- Java Program to convert positive int to negative and negative to positive
- Python - Sum negative and positive values using GroupBy in Pandas
- How to create bar plot with positive and negative values in R?
- True or false1)every integer is either positive or negative
- Counting number of positive and negative votes in MySQL?
- Display the sum of positive and negative values from a column in separate columns with MySQL
- How to check whether a number is Positive or Negative in Golang?
- Test element-wise for positive or negative infinity in Numpy
- C program to Check Whether a Number is Positive or Negative or Zero?

Advertisements