Found 4378 Articles for MySQL

Update all varchar column rows to display values before slash in MySQL?

AmitDiwan
Updated on 20-Nov-2020 07:29:12

175 Views

For this, use UPDATE command along with SUBSTRING_INDEX(). Let us first create a table −mysql> create table demo69 −> ( −> name varchar(40) −> ); Query OK, 0 rows affected (5.04 sec)Insert some records into the table with the help of insert command −mysql> insert into demo69 values('John/Smith'); Query OK, 1 row affected (0.83 sec) mysql> insert into demo69 values('David/Miller'); Query OK, 1 row affected (0.23 sec) mysql> insert into demo69 values('Chris/Brown'); Query OK, 1 row affected (0.40 sec) mysql> insert into demo69 values('Carol/Taylor'); Query OK, 1 row affected (0.36 sec)Display records from the table using select ... Read More

How to combine few row records in MySQL?

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

66 Views

For this, use CASE WHEN concept. Let us first create a table −mysql> create table demo68 −> ( −> id int not null auto_increment primary key, −> company_name varchar(50), −> employee_name varchar(50), −> country_name varchar(50) −> ); Query OK, 0 rows affected (1.86 sec)Insert some records into the table with the help of insert command −mysql> insert into demo68(company_name, employee_name, country_name) values('Google', 'John', 'US'); Query OK, 1 row affected (0.29 sec) mysql> insert into demo68(company_name, employee_name, country_name) values('Google', 'Bob', 'UK'); Query OK, 1 row affected (0.10 sec) mysql> insert into demo68(company_name, employee_name, country_name) values('Google', 'David', 'AUS'); Query OK, ... Read More

MySQL limit in a range fail to display first 3 row?

AmitDiwan
Updated on 20-Nov-2020 07:25:50

90 Views

Following is the syntax to display only the first 3 rows with LIMIT set in a range −select *from yourTableName limit yourStartIndex, yourEndIndex;Let us first create a table −mysql> create table demo67 −> ( −> id int, −> user_name varchar(40), −> user_country_name varchar(20) −> ); Query OK, 0 rows affected (0.72 sec)Insert some records into the table with the help of insert command −mysql> insert into demo67 values(10, 'John', 'US'); Query OK, 1 row affected (0.19 sec) mysql> insert into demo67 values(1001, 'David', 'AUS'); Query OK, 1 row affected (0.14 sec) mysql> insert into demo67 values(101, 'Mike', 'UK'); ... Read More

MySQL database field type for search query?

AmitDiwan
Updated on 20-Nov-2020 07:21:08

116 Views

Following is the syntax −select *from yourTableName where REGEXP_INSTR(yourColumnName, yourSearchValue);To understand the above syntax, let us first create a table −mysql> create table demo64 −> ( −> id int not null auto_increment primary key, −> name varchar(40) −> ); Query OK, 0 rows affected (3.06 sec)Insert some records into the table with the help of insert command −mysql> insert into demo64(name) values('John Smith'); Query OK, 1 row affected (0.21 sec) mysql> insert into demo64(name) values('John Doe'); Query OK, 1 row affected (0.15 sec) mysql> insert into demo64(name) values('Chris Brown'); Query OK, 1 row affected (0.08 sec) mysql> ... Read More

Encrypt and Decrypt a string in MySQL?

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

6K+ Views

To encrypt and decrypt in MySQL, use the AES_ENCRYPT() and AES_DECRYPT() in MySQL −insert into yourTableName values(AES_ENCRYPT(yourValue, yourSecretKey)); select cast(AES_DECRYPT(yourColumnName, yourSecretKey) as char) from yourTableName;To understand the above syntax, let us first create a table −mysql> create table demo63 −> ( −> value blob −> ); Query OK, 0 rows affected (2.60 sec)Insert some records into the table with the help of insert command. We are encrypting while inserting −mysql> insert into demo63 values(AES_ENCRYPT('John', 'PASS')); Query OK, 1 row affected (0.18 sec) mysql> insert into demo63 values(AES_ENCRYPT('David', 'PASS')); Query OK, 1 row affected (0.41 sec) mysql> insert ... Read More

How to add a row to a table using only strings from another table as reference in MySQL?

AmitDiwan
Updated on 20-Nov-2020 07:16:20

147 Views

For this, use INSERT INTO SELECT statement in MySQL. Let us create a table −mysql> create table demo61 −> ( −> id int, −> name varchar(20) −> ) −> ; Query OK, 0 rows affected (1.84 sec)Insert some records into the table with the help of insert command −mysql> insert into demo61 values(1, 'John'); Query OK, 1 row affected (0.63 sec) mysql> insert into demo61 values(2, 'David'); Query OK, 1 row affected (0.13 sec) mysql> insert into demo61 values(1, 'Mike'); Query OK, 1 row affected (0.10 sec) mysql> insert into demo61 values(2, 'Carol'); Query OK, 1 row ... Read More

SELECT WHERE IN null in MySQL?

AmitDiwan
Updated on 20-Nov-2020 07:14:04

401 Views

Following is the syntax −select yourColumnName1, yourColumnName2, yourColumnName3, . . . N from yourTableName where yourValue in(yourColumnName1, yourColumnName2) or yourColumnName1 is NULL;Let us create a table −mysql> create table demo60 −> ( −> id int not null auto_increment primary key, −> first_name varchar(20), −> last_name varchar(20) −> ) −> ; Query OK, 0 rows affected (2.11 sec)Insert some records into the table with the help of insert command −mysql> insert into demo60(first_name, last_name) values('John', 'Smith'); Query OK, 1 row affected (0.09 sec) mysql> insert into demo60(first_name, last_name) values('John', 'Doe'); Query OK, 1 row affected (0.51 sec) mysql> insert ... Read More

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

Advertisements