Jennifer Nicholas has Published 291 Articles

Search for a string within text column in MySQL?

Jennifer Nicholas

Jennifer Nicholas

Updated on 30-Jul-2019 22:30:24

995 Views

You can search for a string within text column in MySQL with the help of LIKE clause. The syntax is as follows −select *from yourTableName where yourColumnName like '%anyStringValue%';To use the above syntax, let us first create a table −mysql> create table SearchTextDemo    −> (    −> BookName TEXT ... Read More

How to get primary key of a table in MySQL?

Jennifer Nicholas

Jennifer Nicholas

Updated on 30-Jul-2019 22:30:24

4K+ Views

To get the primary key of a table, you can use the show command. The syntax is as follows −SHOW INDEX FROM yourDatebaseName.yourTableName WHERE Key_name = 'PRIMARY';Suppose, we have a table with two primary keys; one of them is “Id” and second is “RollNum". The query for a table is ... Read More

Insert current date in datetime format MySQL?

Jennifer Nicholas

Jennifer Nicholas

Updated on 30-Jul-2019 22:30:24

956 Views

To insert the current date (not time), then you can use in-built function CURDATE() from MySQL. The syntax is as follows −INSERT INTO yourTableName values(curdate());Or if you want to add date and time both then you can use the in-built function NOW() from MySQL. The syntax is as follows −INSERT ... Read More

Concatenate two columns in MySQL?

Jennifer Nicholas

Jennifer Nicholas

Updated on 30-Jul-2019 22:30:24

8K+ Views

To concatenate two columns, use CONCAT() function in MySQL. The syntax is as follows −select CONCAT(yourColumnName1, ' ', yourColumnName2) as anyVariableName from yourTableName;To understand the above concept, let us create a table. The query to create a table is as follows −mysql> create table concatenateTwoColumnsDemo    −> (    −> ... Read More

How to bulk update MySQL data with a single query?

Jennifer Nicholas

Jennifer Nicholas

Updated on 30-Jul-2019 22:30:24

7K+ Views

You can bulk update MySQL data with one query using CASE command. The syntax is as follows −update yourTableName set yourUpdateColumnName = ( Case yourConditionColumnName WHEN Value1 THEN ‘’UpdatedValue’ WHEN Value2 THEN ‘UpdatedValue’ . . N END) where yourConditionColumnName IN(Value1, Value2, .....N);To understand the above concept, let us create a ... Read More

MySQL syntax not evaluating with not equal operator in presence of null?

Jennifer Nicholas

Jennifer Nicholas

Updated on 30-Jul-2019 22:30:24

164 Views

Use the IS NOT NULL operator to compare with NULL values. The syntax is as follows −SELECT *FROM yourTableName where yourColumnName1 is not null or yourColumnName2 anyIntegerValue;To check the not equal to in presence of null, let us create a table. The query to create a table is as ... Read More

Can we update MySQL with if condition?

Jennifer Nicholas

Jennifer Nicholas

Updated on 30-Jul-2019 22:30:24

722 Views

You can update MySQL with IF condition as well as CASE statement. For this purpose, let us first create a table. The query to create a table −mysql> create table UpdateWithIfCondition    −> (    −> BookId int,    −> BookName varchar(200)    −> ); Query OK, 0 rows affected ... Read More

How to get the Average on MySQL time column?

Jennifer Nicholas

Jennifer Nicholas

Updated on 30-Jul-2019 22:30:24

2K+ Views

To get average on time column, use the below syntax. It will give the average in time format −SELECT SEC_TO_TIME(AVG(TIME_TO_SEC(yourColumnName))) as anyVariableName from yourTableName;To understand the above concept, let us create a table. The following is the query −mysql> create table AverageOnTime    −> (    −> PunchInTime time   ... Read More

Increase and decrease row value by 1 in MySQL with Stored Procedure?

Jennifer Nicholas

Jennifer Nicholas

Updated on 30-Jul-2019 22:30:24

659 Views

Let us first create a table to increase and adecrease row value by 1. The following is the query −mysql> create table IncrementAndDecrementValue    −> (    −> UserId int,    −> UserScores int    −> ); Query OK, 0 rows affected (0.60 sec)Insert some records in the table using ... Read More

MySQL difference between two timestamps in Seconds?

Jennifer Nicholas

Jennifer Nicholas

Updated on 30-Jul-2019 22:30:24

288 Views

You can use in-built function UNIX_TIMESTAMP() from MySQL to get the timestamps and the difference between two timestamps. The syntax is as follows −SELECT UNIX_TIMESTAMP(yourColumnName1) - UNIX_TIMESTAMP(yourColumnName2) as anyVariableName from yourTableName;To understand the above concept, let us create a table. The following is the query to create a table −mysql> ... Read More

Advertisements