Best Datatype for Currencies in MySQL

Arjun Thakur
Updated on 30-Jun-2020 07:03:33

198 Views

The best data type for currencies in MySQL is a DECIMAL. The syntax of DECIMAL data type is as follows −DECIMAL(TotalDigit, NumberOfDigitAfterDecimalPoint);To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table CurrenciesDemo    -> (    -> TotalPrice DECIMAL(10, 2)    -> ); Query OK, 0 rows affected (1.82 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into CurrenciesDemo values(1647575.67); Query OK, 1 row affected (0.19 sec) mysql> insert into CurrenciesDemo values(1647575); Query OK, 1 row affected (0.21 sec) ... Read More

MySQL BETWEEN Without Endpoints

Ankith Reddy
Updated on 30-Jun-2020 07:01:10

255 Views

If you do not want to include the end value in between, then use the following syntax −SELECT * FROM yourTableName WHERE yourColumnName BETWEEN yourStartingValue and yourEndingValue and    yourColumnName not in (yourEndingValue );To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table BetweenWithoutEndPoints    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> Name varchar(20),    -> Age int,    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.54 sec)Now you can insert some records in the table using insert command. The ... Read More

MySQL BETWEEN Clause Without Beginning and Endpoints

Chandu yadav
Updated on 30-Jun-2020 06:56:34

88 Views

If you do not want to include start and end value in between, then use the following syntax −SELECT * FROM yourTableName WHERE yourColumnName BETWEEN yourStartingValue and yourEndingValue and    yourColumnName not in (yourStartingValue , yourEndingValue );To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table BetweenWithoutEndPoints    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> Name varchar(20),    -> Age int,    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.54 sec)Now you can insert some records in the table using ... Read More

Select Distinct Combinations from Two Columns in MySQL

George John
Updated on 30-Jun-2020 06:53:01

4K+ Views

To select distinct combinations from two columns, you can use CASE statement. Let us create a table with some columns.The query to create a table is as follows −mysql> create table select_DistinctTwoColumns    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> FirstValue char(1),    -> SecondValue char(1),    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.57 sec)Now you can insert some records in the table using insert command. The query is as follows −mysql> insert into select_DistinctTwoColumns(FirstValue, SecondValue) values('s', 't'); Query OK, 1 row affected (0.12 sec) mysql> insert into select_DistinctTwoColumns(FirstValue, SecondValue) ... Read More

Insert Sequential Number in MySQL

Chandu yadav
Updated on 30-Jun-2020 06:49:28

1K+ Views

You can insert sequential number in MySQL using session variable. The syntax is as follows −SELECT @anyVariableName − = anyIntegerValue; UPDATE yourTableName SET yourColumnName = @anyVariableName − = @anyVariableName+IncrementStep;To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table SequentialNumberDemo    -> (    -> SequentialNumber int not null    -> ); Query OK, 0 rows affected (0.84 sec)Insert records in the table using insert command. The query is as follows −mysql> insert into SequentialNumberDemo values(100); Query OK, 1 row affected (0.11 sec) mysql> insert into SequentialNumberDemo values(10); ... Read More

Order By Last 3 Characters in MySQL

George John
Updated on 30-Jun-2020 06:48:25

19K+ Views

You can use ORDER BY RIGHT() function to order by last 3 chars in MySQL. The syntax is as follows −SELECT *FROM yourTableName ORDER BY RIGHT(yourColumnName, 3) yourSortingOrder;Just replace the ‘yourSortingOrder’ to ASC or DESC to set the ascending or descending order respectively.To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table OrderByLast3Chars    -> (    -> EmployeeId int NOT NULL AUTO_INCREMENT,    -> EmployeeName varchar(20),    -> EmployeeAge int,    -> PRIMARY KEY(EmployeeId)    -> ); Query OK, 0 rows affected (0.56 sec)Insert some records in ... Read More

Add Static Value While Inserting into MySQL with Select

Chandu yadav
Updated on 30-Jun-2020 06:47:02

13K+ Views

You can add static value when you use INSERT INTO SELECT MySQL query. Write the value directly in the select statement or you can add with the help of variable which initializes the value.Case 1 − Place the value directly in the INSERT INTO SELECT statement. The syntax is as follows −INSERT INTO yourSecondTableName(yourColumnName1, yourColumnName2, ....N)    SELECT yourColumnName1 ,yourColumnName2, .....N, yourStaticValue from yourFirstTableName;Case 2 − Add using variable. The syntax is as follows −SET @yourVariableName − = yourstaticValue;    INSERT INTO yourSecondTableName(yourColumnName1, yourColumnName2, ....N)    SELECT yourColumnName1 ,yourColumnName2, .....N, @yourVariableName from yourFirstTableName;To understand the above syntax, you need to ... Read More

Insert Custom Date into MySQL Timestamp Field

Chandu yadav
Updated on 30-Jun-2020 06:43:11

2K+ Views

The problem with UNIX_TIMESTAMP() function is that it returns an integer while we want to insert custom date i.e. not any integer part to MySQL date.Do not use UNIX_TIMESTAMP() for your column defined as TIMESTAMP because UNIX_TIMESTAMP() returns an integer.Check the UNIX_TIMESTAMP. The query is as follows −mysql> select UNIX_TIMESTAMP( '2019-01-09 15 −48 −23') AS IntegerValue;The following is the output −+--------------+ | IntegerValue | +--------------+ | 1547029103   | +--------------+ 1 row in set (0.00 sec)Look at the sample output, the UNIX_TIMESTAMP() function returns an integer of corresponding date and time.The syntax is as follows to insert custom date for ... Read More

MySQL SELECT with WHERE ID in Order by Particular Column

Ankith Reddy
Updated on 30-Jun-2020 06:42:32

5K+ Views

You can SELECT ….WHERE id IN(..) using field() function to order with any column. The syntax is as follows −SELECT *FROM yourTableName WHERE yourColumnName IN(‘value1’, ’value2’, .......N) ORDER BY FIELD(yourColumnName, value1’, ’value2’, .......N);To understand the above syntax, let us create a table −mysql> create table SelectOrderbyField    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> Name varchar(30),    -> Age int,    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into SelectOrderbyField(Name, Age) values('John', 23); Query OK, ... Read More

MySQL ORDER BY Specific Strings

Chandu yadav
Updated on 30-Jun-2020 06:33:31

729 Views

Order by the choice of strings you want, using the FIELD() function. The syntax is as follows −SELECT *FROM yourTableName ORDER BY FIELD(yourColumnName, ’yourValue1’, ’yourValue2’, ’yourValue3’, ....N);To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table OrderByListOfStrings    -> (    -> Id int not null auto_increment,    -> CarName varchar(20),    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.68 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into OrderByListOfStrings(CarName) values('Ford'); Query OK, 1 row affected ... Read More

Advertisements