Found 6702 Articles for Database

Order by a function of two columns in a single MySQL query

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

70 Views

Let us first create a tablemysql> create table orderByAFunctionDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> FirstNumber int,    -> SecodNumber int    -> ); Query OK, 0 rows affected (0.44 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into orderByAFunctionDemo(FirstNumber, SecodNumber) values(10, 4); Query OK, 1 row affected (0.11 sec) mysql> insert into orderByAFunctionDemo(FirstNumber, SecodNumber) values(45, 78); Query OK, 1 row affected (0.17 sec) mysql> insert into orderByAFunctionDemo(FirstNumber, SecodNumber) values(23, 10); Query OK, 1 row affected (0.12 sec) mysql> insert into orderByAFunctionDemo(FirstNumber, SecodNumber) values(67, ... Read More

Display sum in last row of table using MySQL?

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

1K+ Views

To display sum in last row of a table, you can use UNION. To understand how, let us create a tablemysql> create table showSumInLastRowDemo    -> (    -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentName varchar(20),    -> StudentMarks int    -> ); Query OK, 0 rows affected (0.69 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into showSumInLastRowDemo(StudentName, StudentMarks) values('John', 56); Query OK, 1 row affected (0.14 sec) mysql> insert into showSumInLastRowDemo(StudentName, StudentMarks) values('John', 87); Query OK, 1 row affected (0.10 sec) mysql> insert into showSumInLastRowDemo(StudentName, StudentMarks) ... Read More

How to compare two strings which are numbers in MySQL?

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

902 Views

To compare two strings which are numbers in MySQL, use the CAST() function.The syntax is as followsselect *from yourTableName where cast(yourColumnName as signed)=yourIntegerValue;To understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table compareTwoStringDemo -> ( -> UserId varchar(100) -> ); Query OK, 0 rows affected (0.78 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into compareTwoStringDemo values('1083745'); Query OK, 1 row affected (0.12 sec) mysql> insert into compareTwoStringDemo values('9867585'); Query OK, 1 ... Read More

Add new MySQL table columns and create indexes?

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

138 Views

To add a new MySQL table column and index, you can use ALTER TABLE command.The syntax is as followsALTER TABLE yourTableName ADD COLUMN yourColumnName dataType, ADD INDEX(yourColumnName );To understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table AddColumnAndIndexDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(100),    -> Address varchar(200)    -> ); Query OK, 0 rows affected (0.81 sec)Now you can check the description of table. The query is as follows −mysql> desc AddColumnAndIndexDemo;The following is the output+---------+--------------+------+-----+---------+----------------+ | Field ... Read More

How to find absolute difference between two numbers in MySQL?

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

3K+ Views

To get the difference between two number in MySQL, let us first create a demo tablemysql> create table findDifferenceDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> FirstNumber float, -> SecondNumber float -> ); Query OK, 0 rows affected (0.60 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into findDifferenceDemo(FirstNumber, SecondNumber) values(4.2, 2.3); Query OK, 1 row affected (0.20 sec) mysql> insert into findDifferenceDemo(FirstNumber, SecondNumber) values(23.4, 5.6); Query OK, 1 row affected (0.14 sec) ... Read More

How to select unique value in MySQL?

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

2K+ Views

You can select unique value with the help of DISTINCT keyword.The syntax is as followsselect distinct yourColumnName from yourTableName;To understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table selectUniqueValue -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(20), -> Age int -> ); Query OK, 0 rows affected (0.64 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into selectUniqueValue(Name, Age) values('John', 21); ... Read More

How can I enforce compound uniqueness in MySQL?

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

41 Views

You can enforce compound uniqueness in MySQL with the help of UNIQUE keyword. Here is the syntax to add UNIQUE keyword to your table column.The syntax is as followsCREATE TABLE yourTableName (    yourColumnName1 datatype,    yourColumnName2 datatype,    yourColumnName3 datatype,    .    .    N    UNIQUE yourConstarintName(yourColumnName2, yourColumnName3) );To understand the above concept, let us create a table with some columns and add a unique constraint to a table. The query to create a table is as followsmysql> create table UniqueDemo    -> (    -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentName varchar(100), ... Read More

How to create a new table from merging two tables with MySQL union?

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

3K+ Views

The following is the syntax to merge two tables using MySQL unioncreate table yourTableName ( select *from yourTableName1 ) UNION ( select *from yourTableName2 );To understand the above syntax, let us create a table. The query to create first table is as followsmysql> create table Old_TableDemo -> ( -> UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> UserName varchar(20) -> ); Query OK, 0 rows affected (0.63 sec)The query to create second table is as followsmysql> create table Old_TableDemo2 ... Read More

MySQL ORDER BY Date field not in date format?

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

627 Views

The following is the syntax to order by date field which is not in date formatselect *from yourTableName order by STR_TO_DATE(yourColumnName, '%d/%m/%Y') DESC;To understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table orderByDateFormatDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> ArrivalDatetime varchar(100)    -> ); Query OK, 0 rows affected (0.73 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into orderByDateFormatDemo(ArrivalDatetime) values('01/10/2012'); Query OK, 1 row affected (0.20 sec) mysql> insert into orderByDateFormatDemo(ArrivalDatetime) values('03/11/2010'); ... Read More

How to add a number to a current value in MySQL (multiple times at the same time)?

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

300 Views

You can use UPDATE command for this.The syntax is as followsupdate yourTableName set yourColumnName =yourColumnName +yourIntegerValue where ;To understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table addANumberToCurrentValueDemo    -> (    -> Game_Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Game_Score int    -> ); Query OK, 0 rows affected (0.67 sec)Now you can insert some records in the table using insert command. The query is as follows −mysql> insert into addANumberToCurrentValueDemo(Game_Score) values(1090); Query OK, 1 row affected (0.30 sec) mysql> insert into addANumberToCurrentValueDemo(Game_Score) values(204); Query OK, ... Read More

Advertisements