MySQL Articles

Page 322 of 355

MySQL Query to find tables modified in the last hour?

Ankith Reddy
Ankith Reddy
Updated on 30-Jul-2019 533 Views

You can achieve this with the help of INFORMATION_SCHEMA.TABLES. Use the date_sub() with interval. The syntax is as follows −SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE DATE_SUB(NOW(), INTERVAL -1HOUR) < ‘UPDATE_TIME’;Now you can check the above syntax. Here is the query to find the tables modified in the last hour −mysql> select table_name from `INFORMATION_SCHEMA`.`TABLES` -> WHERE -> DATE_SUB(NOW(), INTERVAL 1 HOUR) < `UPDATE_TIME`;Output+---------------------+ | TABLE_NAME          | +---------------------+ | innodb_table_stats  | | innodb_index_stats  | | employeeinformation | +---------------------+ 3 rows in set (0.37 sec)The above query selects only table name. If you want information like table schema, table ...

Read More

MySQL UPDATE the corresponding column with random number between 1-3?

George John
George John
Updated on 30-Jul-2019 1K+ Views

For random numbers in a range, you need to use the RAND() method from MySQL. The syntax is as follows for update −UPDATE yourTableName set yourColumnName=value where yourColumnName2=(SELECT FLOOR(1+RAND()*3));In the above query, the statement FLOOR(1+RAND()*3) generates the number between 1-3 and update the column.To understand the above syntax, let us first create a table. The query to create a table is as follows −mysql> create table updateRowWith1To3 -> ( -> Id int, -> Name varchar(100) -> ); Query OK, 0 rows affected (0.47 sec)Insert some records in the ...

Read More

Count how many rows have the same value in MySQL?

Anvi Jain
Anvi Jain
Updated on 30-Jul-2019 3K+ Views

To count how many rows have the same value using the function COUNT(*) and GROUP BY. The syntax is as follows −SELECT yourColumName1, count(*) as anyVariableName from yourTableName GROUP BY yourColumName1;To understand the above syntax, let us first create a table. The query to create a table is as follows −mysql> create table RowWithSameValue    −> (    −> StudentId int,    −> StudentName varchar(100),    −> StudentMarks int    −> ); Query OK, 0 rows affected (0.55 sec)Insert some records with same value. Here, we have added same marks for more than one student for our example. The query ...

Read More

Search for a string within text column in MySQL?

Jennifer Nicholas
Jennifer Nicholas
Updated on 30-Jul-2019 1K+ 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    −> ); Query OK, 0 rows affected (0.55 sec)Insert some strings in the table. The query is as follows −mysql> insert into SearchTextDemo values('Let us C'); Query OK, 1 row affected (0.28 sec) mysql> insert into SearchTextDemo values('C in Depth'); Query OK, 1 row affected (0.14 sec) ...

Read More

How to change the column position of MySQL table without losing column data?

Rishi Rathor
Rishi Rathor
Updated on 30-Jul-2019 12K+ Views

You can change the column position of MySQL table without losing data with the help of ALTER TABLE command. The syntax is as follows −ALTER TABLE yourTableName MODIFY yourColumnName1 data type AFTER yourColumnName2;To understand the above concept, let us create a table. The query to create a table with some columns is as follows −mysql> create table changeColumnPositionDemo −> ( −> StudentId int, −> StudentAddress varchar(200), −> StudentAge int, −> StudentName varchar(200) −> ); Query OK, 0 rows affected (0.72 sec)Let us insert some data in the table. The query to insert records is as follows -.mysql> insert into changeColumnPositionDemo ...

Read More

How to get primary key of a table in MySQL?

Jennifer Nicholas
Jennifer Nicholas
Updated on 30-Jul-2019 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 as follows −mysql> create table TwoOrMorePrimary    −> (    −> Id int,    −> Name varchar(200),    −> RollNum int    −> ,    −> Primary key(Id, Age)    −> ); Query OK, 0 rows affected (0.85 sec)Apply the above syntax to get primary key of a table. ...

Read More

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

Rishi Rathor
Rishi Rathor
Updated on 30-Jul-2019 2K+ 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

How to front pad zip code with &ldquo;0&rdquo; in MySQL?

Anvi Jain
Anvi Jain
Updated on 30-Jul-2019 598 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

Group month and year in MySQL?

Anvi Jain
Anvi Jain
Updated on 30-Jul-2019 6K+ Views

You can group month and year with the help of function DATE_FORMAT() in MySQL. The GROUP BY clause is also used.The syntax is as follows −SELECT DATE_FORMAT(yourColumnName, '%m-%Y') from yourTableName GROUP BY MONTH(yourColumnName), YEAR(yourColumnName)DESC;To understand the above concept, let us create a table. The following is the query to create a table −mysql> create table GroupMonthAndYearDemo −> ( −> DueDate datetime −> ); Query OK, 0 rows affected (1.49 sec)Insert records in the table using the following query −mysql> insert into GroupMonthAndYearDemo values(now()); Query OK, 1 row affected (0.11 sec) ...

Read More

How can I describe all tables in the database through a single statement in MySQL?

Ankith Reddy
Ankith Reddy
Updated on 30-Jul-2019 2K+ Views

You can use INFORMATION_SCHEMA.COLUMNS to describe all tables in database through a single statement. The syntax is as follows.SELECT *FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA=’yourDatabaseName’\GHere I am using my database sample with two tables.The table names are as follows −mytableyourtableImplement the above syntax for your database. The query is as follows −mysql> select * FROM information_schema.columns WHERE table_schema = 'sample'\GThe following is the output describing the two tables in our database.*************************** 1. row *************************** TABLE_CATALOG: def TABLE_SCHEMA: sample TABLE_NAME: mytable COLUMN_NAME: id ORDINAL_POSITION: 1 COLUMN_DEFAULT: NULL IS_NULLABLE: YES DATA_TYPE: int CHARACTER_MAXIMUM_LENGTH: NULL CHARACTER_OCTET_LENGTH: NULL NUMERIC_PRECISION: 10 NUMERIC_SCALE: 0 DATETIME_PRECISION: NULL CHARACTER_SET_NAME: NULL ...

Read More
Showing 3211–3220 of 3,543 articles
« Prev 1 320 321 322 323 324 355 Next »
Advertisements