Found 6702 Articles for Database

How to store Query Result in a variable using MySQL?

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

8K+ Views

To store query result in a variable with MySQL, use the SET command. The syntax is as follows −SET @anyVariableName = ( yourQuery);To understand the above concept, let us create a table. The following is the query to create a table −mysql> create table QueryResultDemo    −> (    −> Price int    −> ); Query OK, 0 rows affected (0.59 sec)Now let us insert some records into the table. The following is the query to insert records −mysql> insert into QueryResultDemo values(100); Query OK, 1 row affected (0.17 sec) mysql> insert into QueryResultDemo values(20); Query OK, 1 row ... Read More

Compare DATE string with string from MySQL DATETIME field?

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

1K+ Views

You can compare DATE string with string from DATETIME field with the help of DATE() function in MySQL.The syntax is as follows −select *from yourTableName where DATE(yourColumnName) = ’anyDateString’;To understand the above syntax, let us create a table and set some datetime values in the table. The query to create a table −mysql> create table DateTimeDemo −> ( −> ArrivalTime datetime −> ); Query OK, 0 rows affected (0.61 sec)Let us insert some records in the table with the help of insert command. The following is the query to insert records ... Read More

Get the difference between two timestamps in seconds in MySQL?

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

545 Views

To get difference between two timestamps in seconds, use two in-built functions TIME_TO_SEC() and TIMEDIFF() in MySQL. The syntax is as follows −select time_to_sec(timediff(yourCoulnName1, yourCoulnName2)) as anyVariableName from yourTableName;To understand the above concept, let us first create a table. The query to create a table.mysql> create table TimeToSecond −> ( −> MyTime timestamp, −> YourTime timestamp −> ); Query OK, 0 rows affected (0.48 sec)Now you can insert some datetime values in the table. The query is as follows −mysql> insert into TimeToSecond values('2016-05-10 10:02:00', '2016-05-10 10:00:00'); Query ... Read More

Concatenate two columns in MySQL?

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

7K+ 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    −> (    −> StudentId int,    −> StudentName varchar(200),    −> StudentAge int    −> ); Query OK, 0 rows affected (1.06 sec)Now you can insert some records in the table. The query to insert records is as follows −mysql> insert into concatenateTwoColumnsDemo values(1, 'Sam', 21); Query OK, 1 row affected (0.18 sec) ... Read More

How to front pad zip code with “0” in MySQL?

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

437 Views

To front pad zip code with 0, use LPAD() function in MySQL. The syntax is as follows −SELECT LPAD(yourColumnName, columnWidth+1, '0') as anyVariableName from yourTableName;To understand the above concept of LPAD() to add front pad zip code with 0, let us create a table. One of the columns of the table is Zip Code. The following is the query to create a table.mysql> create table ZipCodePadWithZeroDemo    −> (    −> Name varchar(200),    −> YourZipCode int(6)    −> ); Query OK, 0 rows affected (0.44 sec)Insert some records in the table. The query to insert records is as follows ... Read More

Check if a user exists in MySQL and drop it?

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

1K+ Views

To check how many users are present in MySQL, use MySQL.user table. The syntax is as follows to check how many users are present.mysql> SELECT User FROM mysql.user;The following output displays the users −+------------------+ | User | +------------------+ | Mac | | Manish | | mysql.infoschema | | mysql.session | | mysql.sys | | root ... Read More

Do a select in MySQL based only on month and year?

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

1K+ Views

To select MySQL based on month and year, use in-built function YEAR() and MONTH(). The syntax is as follows −select *from yourTableName where YEAR(yourColumnName) = YearValue AND MONTH(yourColumnName) = monthValue;To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table selectDataOnYearandMonthDemo −> ( −> BookId int, −> BookName varchar(100), −> BookDueDate datetime −> ); Query OK, 0 rows affected (0.57 sec)Now you can insert some records in the table. The query is as ... Read More

Insert current date in datetime format MySQL?

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

796 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 INTO yourTableName values(now());To understand both the syntax, let us first create a table. The query to create a table is as follows −mysql> create table NowAndCurdateDemo −> ( −> YourDueDate datetime −> ); Query OK, 0 rows affected (1.75 sec)Implement both ... Read More

MySQL command line client for Windows?

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

3K+ Views

In order to install MySQL command line client for Windows, you need to visit the following URL to get the download link https://dev.mysql.com/downloads/mysql/ −The snapshot is as follows −After that you need to select operating system. The snapshot is as follows −You need to choose Windows (x86, 32/64-bit) and download the installer.

How to display the Engine of a MySQL table?

Vrundesha Joshi
Updated on 25-Jun-2020 12:30:25

1K+ Views

To know whether a MySQL table is using MyISAM or InnoDB engine then you can use below syntax.The below syntax can be used for multiple tables −show table status from yourDatabaseName;Here is the syntax that can be used for a specific table i.e. to know the engine of a table −show table status from yourDatabaseName Like ‘yourTableName’.The following is the query to display engine of all the tables −mysql> show table status from sampleTest;The following is the output −+--------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+--------------------+----------+----------------+---------+ | Name          | Engine  | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | ... Read More

Advertisements