AmitDiwan has Published 11365 Articles

MySQL GROUP BY and CONCAT() to display distinct first and last name

AmitDiwan

AmitDiwan

Updated on 25-Sep-2019 11:30:57

1K+ Views

Let us first create a table −mysql> create table DemoTable (    FirstName varchar(100),    LastName varchar(100) ); Query OK, 0 rows affected (0.92 sec) mysql> alter table DemoTable add index(FirstName, LastName); Query OK, 0 rows affected (1.00 sec) Records: 0 Duplicates: 0 Warnings: 0Insert some records in the table ... Read More

Select distinct names from two columns in MySQL and display the result in a single column

AmitDiwan

AmitDiwan

Updated on 25-Sep-2019 11:25:54

531 Views

For this, use UNION. Let us first create a table −mysql> create table DemoTable (    Name1 varchar(100),    Name2 varchar(100) ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert commandmysql> insert into DemoTable values('Adam', 'Bob'); Query OK, 1 row affected (0.21 sec) mysql> ... Read More

Update 'a' record with 'b' and 'b' with 'a' in a MySQL column (swap) with only 'a' and 'b' values?

AmitDiwan

AmitDiwan

Updated on 25-Sep-2019 11:11:22

343 Views

For this, use a CASE statement. Let us first create a table −mysql> create table DemoTable (    Value char(1) ); Query OK, 0 rows affected (1.21 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('a'); Query OK, 1 row affected (0.16 sec) mysql> insert ... Read More

Can we use INTERVAL keyword while inserting date records in a MySQL table?

AmitDiwan

AmitDiwan

Updated on 25-Sep-2019 11:09:28

223 Views

Yes, we can use INTERVAL while inserting data records. Let us first create a table −mysql> create table DemoTable (    ArrivalTime datetime ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert command. Here, we are using INTERVAL keyword for incrementing the date records ... Read More

Can we use “year” as a column came in a MySQL Table?

AmitDiwan

AmitDiwan

Updated on 25-Sep-2019 11:02:35

101 Views

Yes, you can give the year as a column name in MySQL table since it isn’t a reserved word. Let us first create a table −mysql> create table DemoTable (    Year int ); Query OK, 0 rows affected (0.87 sec)Insert some records in the table using insert command −mysql> ... Read More

Concatenate rows on the basis of boolean values in another column with MySQL

AmitDiwan

AmitDiwan

Updated on 25-Sep-2019 11:00:57

278 Views

To concatenate rows on the basis of boolean value in another column, use GROUP_CONCAT(). Let us first create a table. Here, we have set one of the columns “isValidUser” as BOOLEAN −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    UserMessage varchar(100),    isValidUser boolean ... Read More

MySQL ORDER BY letters (not numbers) for column values comprising strings with numbers like '456 John Smith'

AmitDiwan

AmitDiwan

Updated on 25-Sep-2019 10:53:49

86 Views

To ORDER BY letters, use ORDER BY SUBSTRING(). Let us first create a table −mysql> create table DemoTable (    Id varchar(100) ); Query OK, 0 rows affected (0.65 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('456 John Smith'); Query OK, 1 row affected ... Read More

Two ways to fetch maximum value from a MySQL column with numbers

AmitDiwan

AmitDiwan

Updated on 25-Sep-2019 10:50:23

124 Views

To fetch the maximum value, use any of the below-given syntaxes −select max(yourColumnName) from yourTableName; OR select *from yourTableName order by yourColumnName desc limit 1;Let us first create a table −mysql> create table DemoTable (    Value int ); Query OK, 0 rows affected (0.84 sec)Insert some records in the ... Read More

MySQL query for alphabetical search (ABC) with REGEXP?

AmitDiwan

AmitDiwan

Updated on 25-Sep-2019 10:48:04

390 Views

For alphabetic search, use the REGEX in MySQL. Here, let’s say we are searching for records beginning with A, B or C. The syntax to use REGEXP for the same purpose is as follows −select *from yourTableName where yourColumnName REGEXP '^[ABC]';Let us first create a table −mysql> create table DemoTable ... Read More

Use TRIM on all records in a MySQL table?

AmitDiwan

AmitDiwan

Updated on 25-Sep-2019 10:46:39

458 Views

TRIM is used to remove leading and trailing spaces. Let us first create a table −mysql> create table DemoTable (    StudentName varchar(100) ); Query OK, 0 rows affected (0.64 sec)Insert some records in the table using insert command. Here, we have inserted records with leading and trailing whitespaces −mysql> ... Read More

Advertisements