Found 4219 Articles for MySQLi

Change value of decimal(19, 2) when inserting into the database in MySQL?

AmitDiwan
Updated on 20-Nov-2020 07:10:41

143 Views

To store the exact real value, you need to use truncate() with 2 decimal point. Let us create a table −Following is the query to create a table.mysql> create table demo59 −> ( −> price decimal(19, 2) −> ); Query OK, 0 rows affected (1.12 sec)Insert some records into the table with the help of insert command −mysql> insert into demo59 values(truncate(15.346, 2)); Query OK, 1 row affected (0.14 sec) mysql> insert into demo59 values(truncate(20.379, 2)); Query OK, 1 row affected (0.72 sec) mysql> insert into demo59 values(truncate(25.555, 2)); Query OK, 1 row affected (0.16 sec) mysql> ... Read More

Expression in CASE WHEN Clause doesn't work in MySQL query?

AmitDiwan
Updated on 20-Nov-2020 07:09:22

183 Views

Fir this, use CASE WHEN statement in MySQL correctly. Let us see how.Let us create a table −mysql> create table demo58 −> ( −> id int not null auto_increment primary key, −> first_name varchar(20), −> last_name varchar(20) −> ); Query OK, 0 rows affected (2.15 sec)Insert some records into the table with the help of insert command −mysql> insert into demo58(first_name, last_name) values('John', 'Doe'); Query OK, 1 row affected (0.12 sec) mysql> insert into demo58(first_name, last_name) values('David', 'Smith'); Query OK, 1 row affected (0.29 sec) mysql> insert into demo58(first_name, last_name) values('John', 'Brown'); Query OK, 1 row affected (0.11 ... Read More

How to sort a particular value at the end in MySQL?

AmitDiwan
Updated on 20-Nov-2020 07:06:57

101 Views

For this, you can use ORDER BY. Let us create a table −mysql> create table demo57 −> ( −> id int not null auto_increment primary key, −> full_name varchar(20) −> ); Query OK, 0 rows affected (1.60 sec)Insert some records into the table with the help of insert command −mysql> insert into demo57(full_name) values('John Smith'); Query OK, 1 row affected (0.24 sec) mysql> insert into demo57(full_name) values('David Miller'); Query OK, 1 row affected (0.13 sec) mysql> insert into demo57(full_name) values('Not Known'); Query OK, 1 row affected (0.13 sec) mysql> insert into demo57(full_name) values('Chris Brown'); Query OK, 1 ... Read More

Multiple data input at the same time in MySQL?

AmitDiwan
Updated on 20-Nov-2020 07:05:18

673 Views

Following is the syntax −insert into yourTableName values(yourValue1, yourValue2, .....N), (yourValue1, yourValue2, .....N), (yourValue1, yourValue2, .....N), (yourValue1, yourValue2, .....N), . . . NLet us create a table −mysql> create table demo56 −> ( −> id int, −> first_name varchar(20), −> last_name varchar(20), −> age int −> ); Query OK, 0 rows affected (1.91 sec)Insert some records into the table with the help of insert command −mysql> insert into demo56 values(1, 'John', 'Smith', 23), −> (2, 'David', 'Miller', 21), −> (3, 'Chris', 'Brown', 22), −> (4, 'Carol', 'Taylor', 20); Query OK, 4 rows affected (0.10 sec) Records: 4 Duplicates: 0 Warnings: ... Read More

Update data in one table from data in another table in MySQL?

AmitDiwan
Updated on 20-Nov-2020 07:03:10

1K+ Views

For this, you can use UPDATE command along with JOIN.Let us create the first table −mysql> create table demo54 −> ( −> firstName varchar(20), −> lastName varchar(20) −> ); Query OK, 0 rows affected (0.57 sec)Insert some records into the table with the help of insert command −mysql> insert into demo54 values('John', 'Smith'); Query OK, 1 row affected (0.09 sec) mysql> insert into demo54 values('John', 'Smith'); Query OK, 1 row affected (0.09 sec) mysql> insert into demo54 values('David', 'Smith'); Query OK, 1 row affected (0.11 sec)Display records from the table using select statement −mysql> select *from demo54;This will ... Read More

How to join tables and fetch values from a MySQL database?

AmitDiwan
Updated on 19-Nov-2020 13:23:28

361 Views

To join tables, use the JOIN concept in MySQL. At first, let us create two tables.Let us create the first table −mysql> CREATE TABLE `demo52` ( −> `id` INT NOT NULL, −> `name` VARCHAR(20) NOT NULL, −> PRIMARY KEY (`id`) −> ); Query OK, 0 rows affected (1.19 sec)Insert some records into the table with the help of insert command −mysql> insert into demo52 values(1, 'John'); Query OK, 1 row affected (0.17 sec) mysql> insert into demo52 values(2, 'David'); Query OK, 1 row affected (0.10 sec) mysql> insert into demo52 values(3, 'Mike'); Query OK, 1 row affected (0.13 ... Read More

Placing order according to the condition in MySQL?

AmitDiwan
Updated on 19-Nov-2020 13:22:09

32 Views

For this, use ORDER BY CASE WHEN statement.Let us create a table −mysql> create table demo51 −> ( −> id int not null auto_increment primary key, −> name varchar(20) −> ); Query OK, 0 rows affected (1.08 sec)Insert some records into the table with the help of insert command −mysql> insert into demo51(name) values('John'); Query OK, 1 row affected (0.15 sec) mysql> insert into demo51(name) values('Bob'); Query OK, 1 row affected (0.09 sec) mysql> insert into demo51(name) values('David'); Query OK, 1 row affected (0.35 sec) mysql> insert into demo51(name) values('Sam'); Query OK, 1 row affected (0.14 sec)Display ... Read More

Sum of digits of year in MySQL?

AmitDiwan
Updated on 19-Nov-2020 13:20:28

138 Views

At first, you need to extract the last digit and add the extracted value. And the same goes in till we get the sum of all digits of the year, for example, for the year 2020 −2 + 0 + 2 + 0 = 4The concept is as follows to extract the last digit from the year. Following is the query −select floor(@yourVariableName % 10);Following is the query to sum the digits of year −mysql> set @year_column_value = 2020; Query OK, 0 rows affected (0.00 sec) mysql> select −> floor(@year_column_value / 1000) −> + floor(@year_column_value % 1000 / 100) −> ... Read More

Creating a table with a TIMESTAMP field in MySQL?

AmitDiwan
Updated on 19-Nov-2020 13:18:46

6K+ Views

For this, you can use TIMESTAMP keyword in MySQL.Let us create a table −mysql> create table demo50 −> ( −> id int not null auto_increment primary key, −> start_date timestamp default current_timestamp not null, −> end_date timestamp default current_timestamp not null −> ); Query OK, 0 rows affected (1.35 sec)Insert some records into the table with the help of insert command −mysql> insert into demo50 values(); Query OK, 1 row affected (0.15 sec) mysql> insert into demo50(end_date) values('2020−12−21'); Query OK, 1 row affected (0.07 sec) mysql> insert into demo50(start_date) values('2020−01−01'); Query OK, 1 row affected (0.14 sec)Display records ... Read More

Select all records if it contains specific number in MySQL?

AmitDiwan
Updated on 19-Nov-2020 13:15:53

163 Views

For this, use concat() along with LIKE. Following is the syntax −select *from yourTableName where concat(', ', yourColumnName, ', ') like '%, yourValue, %';Let us create a table −mysql> create table demo49 −> ( −> id varchar(20) −> , −> first_name varchar(20) −> ); Query OK, 0 rows affected (1.45 sec)Insert some records into the table with the help of insert command −mysql> insert into demo49 values('4, 5, 6', −> 'Adam'); Query OK, 1 row affected (0.20 sec) mysql> insert into demo49 values('5, 3, 2', 'Mike'); Query OK, 1 row affected (0.19 sec) mysql> insert into demo49 values('3, ... Read More

Advertisements