AmitDiwan has Published 10740 Articles

Dynamically choosing a column in MySQL?

AmitDiwan

AmitDiwan

Updated on 25-Sep-2019 11:36:44

1K+ Views

First, you need to prepare a query and then you need to execute the PREPARED statement to dynamically choose a column in MySQL.Let us first create a table −mysql> create table DemoTable (    EmployeeName varchar(100) ); Query OK, 0 rows affected (0.77 sec)Insert some records in the table using ... Read More

How to filter dates in MySQL to fetch date record only for a specific month?

AmitDiwan

AmitDiwan

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

369 Views

Let us first create a table −mysql> create table DemoTable (    AdmissionDate varchar(100) ); Query OK, 0 rows affected (1.06 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2018-01-21'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('2019-08-13'); Query OK, 1 ... Read More

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

670 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

482 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

294 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

240 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

396 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

166 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

201 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

Advertisements