Found 4378 Articles for MySQL

Changing data type from date to date/time in MySQL?

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

513 Views

To change data type from date to date/time, use alter command.alter table yourTableName change yourColumnName yourColumnName datetime;Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    LoginDate date    ); Query OK, 0 rows affected (1.26 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(LoginDate) values('2019-01-21'); Query OK, 1 row affected (0.29 sec) mysql> insert into DemoTable(LoginDate) values('2018-05-01'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(LoginDate) values('2017-12-31'); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement ... Read More

MySQL Select displaying Data type in a separate column?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

67 Views

You can use INFORMATION_SCHEMA.COLUMNS for this. Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Name varchar(20)    ); Query OK, 0 rows affected (0.73 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Name) values('Chris'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable(Name) values('Robert'); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable(Name) values('Sam'); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following ... Read More

How can fetch records from specific month and year in a MySQL table?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

561 Views

Use YEAR() and MONTH() to display records from specific month and year respectively. Let us first create a table −mysql> create table DemoTable    (    CustomerId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    CustomerName varchar(20),    CustomerTotalBill int,    PurchasingDate date    ); Query OK, 0 rows affected (0.83 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(CustomerName, CustomerTotalBill, PurchasingDate) values('John', 2000, '2019-01-21'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(CustomerName, CustomerTotalBill, PurchasingDate) values('Chris', 1000, '2019-01-31'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable(CustomerName, CustomerTotalBill, PurchasingDate) values('Robert', ... Read More

How to make MySQL table primary key auto increment?

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

1K+ Views

To make MySQL table primary key auto increment, use the below syntaxCREATE TABLE yourTableName    (    yourColumnName INT(6) ZEROFILL NOT NULL AUTO_INCREMENT,    PRIMARY KEY(yourColumnName)    );Let us first create a table and set primary key auto increment −mysql> CREATE TABLE DemoTable    (    UserId INT(6) ZEROFILL NOT NULL AUTO_INCREMENT,    PRIMARY KEY(UserId)    ); Query OK, 0 rows affected (0.81 sec)Insert some records in the table using insert command −mysql> INSERT INTO DemoTable values(); Query OK, 1 row affected (0.12 sec) mysql> INSERT INTO DemoTable values(); Query OK, 1 row affected (0.13 sec) mysql> INSERT ... Read More

How to search by specific pattern in MySQL?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

115 Views

You can use regular expression for this. Let us first create a table −mysql> create table DemoTable    (    UserId varchar(100)    ); Query OK, 0 rows affected (1.28 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('User-123-G'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Us-453-GO'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values('TRUE-908-K'); Query OK, 1 row affected (0.20 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+------------+ | UserId     | ... Read More

How to prevent a user from accessing a specific schema in MySQL?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

382 Views

To prevent a user from accessing a specific schema, you need to use delete command. Following is the syntax −DELETE FROM mysql.db WHERE Db IN("yourSpecificSchema", "yourSpecificSchema\_%")    AND User = "yourUserName" AND Host = "yourHostName";Let us implement the above syntax to prevent a user from accessing a specific schema. First of all, let us display all users and host from MySQL.user table.mysql> select user, host from MySQL.user;This will produce the following output −+------------------+-----------+ | user             | host      | +------------------+-----------+ | Bob              | %       ... Read More

Implement Conditional MySQL Query in a stored procedure?

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

535 Views

For conditional MySQL query, you can use IF ELSE concept in stored procedure. Let us first create a table −mysql> create table DemoTable1    (    Id int    ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values(10); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable1 values(20); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1 values(30); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select *from DemoTable1;This will produce the following output ... Read More

Preserve select order within MySQL UNION?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

194 Views

It’s a good choice to use CASE statement. Do not use UNION. Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    ShippingDate datetime    ); Query OK, 0 rows affected (0.63 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(ShippingDate) values('2019-04-21'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(ShippingDate) values('2019-01-01'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(ShippingDate) values('2019-05-11'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(ShippingDate) values('2018-12-31'); Query OK, 1 row ... Read More

Select last 3 rows from database order by id ASC?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

324 Views

You can use subquery. Following is the syntax −SELECT * FROM (    SELECT * FROM yourTableName ORDER BY yourIdColumnName DESC LIMIT 3 ) anyAliasName ORDER BY yourIdColumnName;Let us first create a table −mysql> create table DemoTable (    ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    ClientName varchar(100) ); Query OK,  0 rows affected (0.60 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(ClientName) values('Larry'); Query OK,  1 row affected (0.18 sec) mysql> insert into DemoTable(ClientName) values('Chris'); Query OK,  1 row affected (0.12 sec) mysql> insert into DemoTable(ClientName) values('Bob'); Query OK,  1 row affected (0.10 sec) mysql> insert into DemoTable(ClientName) values('David'); Query OK,  1 row affected (0.12 sec) mysql> insert into DemoTable(ClientName) values('Carol'); Query OK,  1 row affected (0.10 sec) mysql> insert into DemoTable(ClientName) values('Robert'); Query OK,  1 row affected (0.19 sec) mysql> insert into DemoTable(ClientName) values('Sam'); Query OK,  1 row affected (0.17 sec) mysql> insert into DemoTable(ClientName) values('Mike'); Query OK,  1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce ... Read More

New line separator doesn't work for group_concat function in MySQL? How to use it correctly?

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

1K+ Views

To use new line separator in group_concat() function, follow the below syntax −select group_concat(concat_ws(' ', yourColumnName1, yourColumnName2) SEPARATOR "\r") from yourTableName;Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    FirstName varchar(20),    LastName varchar(20)    ); Query OK, 0 rows affected (0.67 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(FirstName, LastName) values('John', 'Smith'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(FirstName, LastName) values('David', 'Miller'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(FirstName, LastName) values('John', 'Doe'); ... Read More

Advertisements