Found 6702 Articles for Database

Change value from 1 to Y in MySQL Select Statement using CASE?

Ankith Reddy
Updated on 30-Jul-2019 22:30:24

103 Views

You can use CASE from MySQL to change value from 1 to Y. Let us first create a table. The query to create a table is as follows:mysql> create table changeValuefrom1toY    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> isValidAddress tinyint(1),    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.76 sec)Now you can insert some records in the table using insert command. The query is as follows:mysql> insert into changeValuefrom1toY(isValidAddress) values(1); Query OK, 1 row affected (0.22 sec) mysql> insert into changeValuefrom1toY(isValidAddress) values(0); Query OK, 1 row affected (0.16 sec) mysql> insert ... Read More

Change value from 1 to Y in MySQL Select Statement?

George John
Updated on 30-Jul-2019 22:30:24

122 Views

You can use IF() from MySQL to change value from 1 to Y. The syntax is as follows:SELECT IF(yourColumnName, ’Y’, yourColumnName) as anyVariableName FROM yourTableName;To understand the above syntax, let us create a table. The query to create a table is as follows:mysql> create table changeValuefrom1toY    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> isValidAddress tinyint(1),    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.76 sec)Now you can insert some records in the table using insert command. The query is as follows:mysql> insert into changeValuefrom1toY(isValidAddress) values(1); Query OK, 1 row affected (0.22 ... Read More

MySQL SELECT DISTINCT and count?

Arjun Thakur
Updated on 30-Jul-2019 22:30:24

501 Views

You need to use GROUP BY command with aggregate function count(*) from MySQL to achieve this. The syntax is as follows:SELECT yourColumnName, COUNT(*) AS anyVariableNameFROM yourTableName GROUP BY yourColumnName;To understand the above syntax, let us create a table. The query to create a table is as follows:mysql> create table selectDistinct_CountDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> Name varchar(10),    -> AppearanceId int,    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.63 sec)Insert some records in the table using insert command. The query is as follows:mysql> insert into selectDistinct_CountDemo(Name, AppearanceId) values('Larry', ... Read More

Are quotes around tables and columns in a MySQL query really necessary?

Chandu yadav
Updated on 30-Jul-2019 22:30:24

382 Views

If your table name or column name are any reserved words then you need to use quotes around table name and column name in a MySQL query. You need to use backticks around table name and column name. The syntax is as follows:SELECT *FROM `table` where `where`=condition;Here is the query to create a table without quotes with reserved words. You will get an error since they are predefined reserved words. The error is as follows:mysql> create table table    -> (    -> where int    -> ); ERROR 1064 (42000): You have an error in your SQL syntax; check ... Read More

Execute INSERT if table is empty in MySQL?

Ankith Reddy
Updated on 30-Jul-2019 22:30:24

2K+ Views

You can execute insert if table is empty with the help of subquery. For that, work on not exists condition with subquery.The below syntax will work only when your table is empty. If your table is not empty then it will not insert the record. The syntax is as follows:INSERT INTO yourTableName(yourColumnName) SELECT ‘anyValue’ WHERE NOT EXISTS (SELECT *FROM yourTableName);To understand the above syntax, let us create a table. The query to create a table is as follows:mysql> create table ExecuteInsertDemo    -> (    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.67 sec)Let us insert ... Read More

How do I add to each row in MySQL?

George John
Updated on 30-Jul-2019 22:30:24

363 Views

You can add a value to each row in MySQL using UPDATE command.Let us see when your column is an integer. The syntax is as follows:UPDATE yourTableName SET yourIntegerColumnName = yourIntegerColumnName+anyValue; UPDATE yourTableName SET yourIntegerColumnName = anyValue WHERE yourIntegerColumnName IS NULL;You can add a value for a date column name. The syntax is as follows:UPDATE yourTableName SET yourDateColumnName = DATEADD(yourDateColumnName, INTERVAL anyIntegerMonth)To understand the above syntax, let us create a table. The query to create a table is as follows:mysql> create table addEachRowValue    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> Amount int,    -> ShippingDate ... Read More

Removing NOT NULL restriction from column in MySQL?

Arjun Thakur
Updated on 30-Jul-2019 22:30:24

266 Views

To remove NOT NULL restriction from column in MySQL, use ALTER command. The syntax is as follows:ALTER TABLE yourTableName MODIFY COLUMN yourColumnName dataType;To understand the above syntax, let us create a table. The query to create a table is as follows:mysql> create table NotNullDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> Name varchar(20) NOT NULL,    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.58 sec)In the above table, if you insert the NULL value to the column ‘Name’ then MySQL will give an error of NOT NULL restriction. The query to ... Read More

Can MySQL INT type be non-zero NULL?

Chandu yadav
Updated on 30-Jul-2019 22:30:24

176 Views

You can set the INT column to value NULL.The column INT type a nullable column. The syntax is as follows:INSERT INTO yourTableName(yourIntColumnName) values(NULL);To understand the above syntax, let us create a table. The query to create a table is as follows:mysql> create table nullableIntDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> Price int,    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.80 sec)Insert the record as NULL for a int column ‘Price’. The query is as follows:mysql> insert into nullableIntDemo(Price) values(NULL); Query OK, 1 row affected (0.11 sec) mysql> insert into ... Read More

MySQL Select Rows where two columns do not have the same value?

Ankith Reddy
Updated on 30-Jul-2019 22:30:24

973 Views

You can use != operator from MySQL for this. The syntax is as follows:SELECT *FROM yourTableName WHERE yourColumnName1 !=yourColumnName2 OR (yourColumnName1 IS NULL AND yourColumnName2IS NOT NULL) OR (yourColumnName2 IS NULL AND yourColumnName1 IS NOT NULL);To understand the above syntax, let us create a table. The query to create a table is as follows:mysql> create table selectTwoColumns    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> FirstNumber int,    -> SecondNumber int,    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.87 sec)Insert some records in the table using insert command. The query is ... Read More

Why we mention in MySQL WHERE 1=0?

George John
Updated on 30-Jul-2019 22:30:24

4K+ Views

The condition 1=0 can be used to stop the query from returning any rows. It returns empty set.The syntax is as follows:SELECT *FROM yourTableName WHERE 1=0;To understand the above syntax, let us create a table. The query to create a table is as follows:mysql> create table ConditionDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> Name varchar(10),    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.59 sec)Insert some records in the table using insert command. The query is as follows:mysql> insert into ConditionDemo(Name) values('Larry'); Query OK, 1 row affected (0.10 sec) mysql> ... Read More

Advertisements