Found 4381 Articles for MySQL

MySQL query to get the length of all columns and display the result in a single new column?

AmitDiwan
Updated on 01-Oct-2019 06:52:59

641 Views

To get the length of all columns i.e. the count of all the characters for column values, use char_length(). Let us first create a table. Here, we have two columns, therefore we will calculate for each row consisting of both the values FirstName and LastName −mysql> create table DemoTable (    FirstName varchar(100),    LastName varchar(100) ); Query OK, 0 rows affected (1.07 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris', 'Brown'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('David', 'Miller'); Query OK, 1 row affected (0.11 sec) mysql> insert ... Read More

MySQL query to delete a DATE older than 30 days from another date?

AmitDiwan
Updated on 01-Oct-2019 06:51:08

692 Views

Following is the syntax −delete from yourTableName where yourColumnName < (yourAnotherDateValue - INTERVAL 30 DAY);Let us first create a table −mysql> create table DemoTable (    DueDate date ); Query OK, 0 rows affected (0.68 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-08-25'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('2019-07-01'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('2019-06-20'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('2019-09-02'); Query OK, 1 row affected (0.14 sec)Display all records from the table using select statement ... Read More

MySQL query to display columns name first name, last name as full name in a single column?

AmitDiwan
Updated on 01-Oct-2019 06:49:29

13K+ Views

For this, use CONCAT() method in MySQL. Let us first create a table −mysql> create table DemoTable (    FirstName varchar(50),    LastName varchar(50) ); Query OK, 0 rows affected (0.63 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris', 'Brown'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('David', 'Miller'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('John', 'Doe'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values('Adam', 'Smith'); Query OK, 1 row affected (0.25 sec)Display all records from the table using select statement ... Read More

How to alter column type of multiple columns in a single MySQL query?

AmitDiwan
Updated on 01-Oct-2019 06:47:21

842 Views

To alter column type of multiple columns in a single MySQL query, the syntax is as follows −alter table yourTableName modify column yourColumnName 1 yourDataType1, modify column yourColumnName 2 yourDataType2, . . N;Let us first create a table −mysql> create table DemoTable (    Id varchar(100),    FirstName text,    LastName text ); Query OK, 0 rows affected (0.52 sec)Let us check the description of table −mysql> desc DemoTable;This will produce the following output −+-----------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | ... Read More

Get the count of two table fields in a single MySQL query?

AmitDiwan
Updated on 30-Sep-2019 08:28:18

686 Views

For this, you can use the CASE statement along with SUM(). Here, we will be finding the count of Male and Female records from a column with employee gender values. Let us first create a table −mysql> create table DemoTable (    EmployeeGender ENUM('Male', 'Female') ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Male'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('Female'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Male'); Query OK, 1 row affected (0.10 sec) mysql> insert into ... Read More

What is the usage of “@” symbol in MySQL stored procedure?

AmitDiwan
Updated on 30-Sep-2019 08:26:39

1K+ Views

The @ symbol in a stored procedure can be used for user-defined session variables. Let us first create a table −mysql> create table DemoTable (    StudentName varchar(50) ); Query OK, 0 rows affected (1.30 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John Smith'); Query OK, 1 row affected (1.00 sec) mysql> insert into DemoTable values('John Doe'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('Chris Brown'); Query OK, 1 row affected (0.53 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output ... Read More

Fetch records on the basis of LastName using MySQL IN()

AmitDiwan
Updated on 30-Sep-2019 08:25:09

128 Views

Let us first create a table −mysql> create table DemoTable (    FirstName varchar(100),    LastName varchar(100) ); Query OK, 0 rows affected (0.83 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Adam', 'Smith'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('John', 'Doe'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('John', 'Smith'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Chris', 'Brown'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values('Robert', 'Brown'); Query OK, 1 row affected (0.10 sec) mysql> insert ... Read More

MySQL query to replace only the NULL values from the table?

AmitDiwan
Updated on 30-Sep-2019 08:22:56

142 Views

For this, you can use the property IS NULL for null values in MySQL. Let us first create a table −mysql> create table DemoTable (    Name varchar(100) ); Query OK, 0 rows affected (0.53 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values(null); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(null); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Robert'); Query OK, 1 ... Read More

Display the record with non-duplicate Id using MySQL GROUP BY and HAVING

AmitDiwan
Updated on 30-Sep-2019 08:19:40

150 Views

Let us first create a table −mysql> create table DemoTable (    Id int,    ColorName varchar(100) ); Query OK, 0 rows affected (0.56 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100, 'Red'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(101, 'Green'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(101, 'Blue'); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable values(102, 'Yellow'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(100, 'Purple'); Query OK, 1 row affected (0.12 sec) mysql> insert ... Read More

Delete last value and fix two new values (VARCHAR Numbers) in MySQL declared as VARCHAR?

AmitDiwan
Updated on 30-Sep-2019 08:16:41

100 Views

Let us first create a table. Here, we have VARCHAR type for value −mysql> create table DemoTable (    Value varchar(100) ); Query OK, 0 rows affected (1.80 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('100'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('1244'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('15789'); Query OK, 1 row affected (0.18 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+-------+ | Value | +-------+ | 100 | ... Read More

Advertisements