Found 4378 Articles for MySQL

How can I select only those rows where first digit is a number from 0 to 9 in MySQL?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

994 Views

To select only those rows where first digit is a number from 0 to 9, use RLIKE. Following is the syntax −select *from yourTableName where yourColumnName RLIKE '^[0-9]+'Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    QuestionNumber varchar(200) ); Query OK, 0 rows affected (0.56 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(QuestionNumber) values('1Question'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(QuestionNumber) values('Question2'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(QuestionNumber) values('311Question'); Query OK, 1 row affected (0.13 sec) ... Read More

How to compare DateTime Column with only Date not time in MySQL?

Daniol Thomas
Updated on 30-Jul-2019 22:30:25

2K+ Views

To compare DateTime column with only Date, you need to use the Date() method. Following is the syntax. Below, you need to date in the 'yourDateValue':select *from yourTableName where Date(yourColumnName)='yourDateValue';Let us first create a table:mysql> create table DemoTable (    ArrivalTime datetime ); Query OK, 0 rows affected (0.74 sec)Following is the query to insert some records in the table using insert command:mysql> insert into DemoTable values('2019-01-31 02:34:56'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('2019-04-09 18:20:58'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('2019-05-11 19:45:23'); Query OK, 1 row affected (0.13 ... Read More

How to modify column default value in MySQL?

George John
Updated on 30-Jul-2019 22:30:25

1K+ Views

Let us first create a table −mysql> create table DemoTable (    UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    UserName varchar(20) DEFAULT 'John' ); Query OK, 0 rows affected (0.76 sec)Let us check the description of table −mysql> desc DemoTable;This will produce the following output −+----------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+----------------+ | UserId | int(11) | NO | PRI | NULL ... Read More

Adding new enum column to an existing MySQL table?

Daniol Thomas
Updated on 30-Jul-2019 22:30:25

5K+ Views

To add a new enum column to an existing MySQL table, you can use ALTER command. Following is the syntax:ALTER TABLE yourTableName ADD yourColumnName ENUM('yourValue1', 'yourValue2’....N) NOT NULL;Let us first create a table:mysql> create table DemoTable (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentName varchar(200),    StudentAge int ); Query OK, 0 rows affected (0.62 sec)Check the description of table using DESC command:mysql> DESC DemoTable;This will produce the following output:+-------------+--------------+------+-----+---------+----------------+ | Field       | Type         | Null | Key | Default | Extra          | +-------------+--------------+------+-----+---------+----------------+ | StudentId ... Read More

Can we use stored procedure to insert records into two tables at once in MySQL?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

389 Views

Yes, you can use stored procedure to insert into two tables in a single query. Let us first create a table −mysql> create table DemoTable (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentFirstName varchar(20) ); Query OK, 0 rows affected (0.56 sec)Here is the query to create second table −mysql> create table DemoTable2 (    ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    ClientName varchar(20),    ClientAge int ); Query OK, 0 rows affected (0.76 sec)Following is the query to create stored procedure to insert into two tables created above −mysql> DELIMITER //    mysql> CREATE PROCEDURE ... Read More

MySQL query to get substrings (only the last three characters) from strings?

Daniol Thomas
Updated on 30-Jul-2019 22:30:25

202 Views

For this, you can use SUBSTR() method. Let us first create a table:mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    FirstName varchar(20)); Query OK, 0 rows affected (1.31 sec)Following is the query to insert some records in the table using insert command:mysql> insert into DemoTable(FirstName) values('John'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(FirstName) values('Carol'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable(FirstName) values('Robert'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(FirstName) values('Chris'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(FirstName) values('David'); ... Read More

Get today's date in (YYYY-MM-DD) format in MySQL?

Daniol Thomas
Updated on 30-Jul-2019 22:30:25

3K+ Views

To get today’s date in (YYYY-MM-DD) format in MySQL, you can use CURDATE().Following is the query to get the current date:mysql> SELECT CURDATE();This will produce the following output:+------------+ | CURDATE() | +------------+ | 2019-04-09 | +------------+ 1 row in set (0.00 sec)You can also use NOW() for this. Following is the query:mysql> SELECT DATE(NOW());This will produce the following output+-------------+ | DATE(NOW()) | +-------------+ | 2019-04-09 | +-------------+ 1 row in set (0.00 sec)

Apply MySQL query to each table in a database?

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

203 Views

To apply MySQL query to each table in a database, you can use INFORMATION_SCHEMA.TABLES. Following is the syntax −SELECT SUM(TABLE_ROWS) AS anyAliasName FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA=yourDatabaseName;Let us implement the above syntax to query each table in a database.mysql> SELECT SUM(TABLE_ROWS) AS Total FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA= DATABASE();This will produce the Following output −+-------+ | Total | +-------+ | 1666 | +-------+ 1 row in set (0.01 sec)

MySQL query to get max id from varchar type and the values in numeric?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

266 Views

Use CAST() in MAX() to get max id from varchar type and values in numeric. Let us first create a table. Here, we have a column with varchar type −mysql> create table DemoTable (    UserMarks varchar(20) ); Query OK, 0 rows affected (0.77 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('77'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('98'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('45'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('56'); Query OK, 1 row affected (0.18 ... Read More

How to use COUNT(*) to return a single row instead of multiple?

Daniol Thomas
Updated on 30-Jul-2019 22:30:25

718 Views

You need to use GROUP BY with COUNT(*) for this to group the values and display the count eliminating multiple values. Let us first create a table:mysql> create table DemoTable (Value int); Query OK, 0 rows affected (0.55 sec)Following is the query to insert some records in the table using insert command:mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(30); Query OK, 1 row affected (0.14 sec) mysql> ... Read More

Advertisements