Found 4381 Articles for MySQL

Can we use {} while creating a MySQL table?

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

203 Views

No, you need to use open and close parenthesis like this ( ) while creating a table. Use the below syntax −CREATE TABLE IF NOT EXISTS yourTableName (    yourColumnName1 dataType1,    .    .    .    .    .    N );Let us first create a table −mysql> CREATE TABLE IF NOT EXISTS DemoTable    (    CustomerId int,    CustomerName varchar(20),    CustomerAge int    ,    PRIMARY KEY(CustomerId)    ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(1, 'Chris', 25); Query OK, 1 row ... Read More

Get rows with GROUP BY in MySQL?

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

243 Views

Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Subject varchar(20),    Price int    ); Query OK, 0 rows affected (0.64 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Subject, Price) values('MySQL', 456); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(Subject, Price) values('MySQL', 456); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(Subject, Price) values('MongoDB', 56); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(Subject, Price) values('MongoDB', 60); Query OK, 1 row affected (0.13 sec) mysql> ... Read More

Retrieve from MySQL only if it contains two hyphen symbols?

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

564 Views

For this, use the LIKE operator. Let us first create a table:mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Password varchar(100)    ); Query OK, 0 rows affected (1.27 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Password) values('John@--123'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable(Password) values('---Carol234'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable(Password) values('--David987'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Password) values('Mike----53443'); Query OK, 1 row affected (0.30 sec)Display all records from the table using select ... Read More

MySQL query to fetch date with year and month?

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

333 Views

For year and month date fetching, you can use YEAR() and MONTH() function in MySQL. 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.48 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(ShippingDate) values('2019-01-31'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(ShippingDate) values('2018-12-01'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable(ShippingDate) values('2019-06-02'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable(ShippingDate) values('2019-11-18'); Query OK, 1 row ... Read More

How to select a row where one of several columns equals a certain value in MySQL?

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

426 Views

For this, you can use multiple OR. Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    FirstName varchar(10),    LastName varchar(10),    Age int,    CountryName varchar(10)    ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(FirstName, LastName, Age, CountryName) values('John', 'Smith', 21, 'US'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(FirstName, LastName, Age, CountryName) values('Carol', 'Taylor', 22, 'AUS'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(FirstName, LastName, Age, CountryName) values('David', ... Read More

Get the count of the most frequently occurring values in MySQL?

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

1K+ Views

For this, use the aggregate function COUNT() along with GROUP BY. Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Value int    ); Query OK, 0 rows affected (0.74 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Value) values(976); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Value) values(67); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(Value) values(67); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(Value) values(1); Query OK, 1 row affected (0.27 sec) mysql> ... Read More

MySQL query to get current datetime and only current date

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

409 Views

The query SELECT NOW() gives the current date as well as current time. If you want only current date, use only CURDATE(). Following is the syntax for datetime −SELECT NOW();The syntax for only date.SELECT CURDATE();Let us now implement the above syntax −Case 1 : If you want both current date and time −mysql> SELECT NOW();Output+-----------------------+ | NOW() | +-----------------------+ | 2019-06-02 15 :30 :39 | +-----------------------+ 1 row in set (0.00 sec)Case 2 : If you want the current date only −mysql> SELECT CURDATE();Output+------------+ | CURDATE() | +------------+ | 2019-06-02 | +------------+ 1 row in set (0.00 sec)

Add two weeks to a date in MySQL?

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

789 Views

To add two weeks to a date in MySQL, use DATE_ADD() −insert into yourTableName(yourColumnName) values(date_add(now(), interval 2 week));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.62 sec)Following is the query to get the current date −mysql> select now(); +-----------------------+ | now() | +-----------------------+ | 2019-06-02 10 :36 :49 | +-----------------------+ 1 row in set (0.00 sec)Insert some records in the table using ... Read More

MySQL query to find all rows where ID is divisible by 4?

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

544 Views

Let us first create a table with one of the column as ID −mysql> create table DemoTable    (    ID int,    StudentName varchar(10),    CountryName varchar(20)    ); Query OK, 0 rows affected (0.70 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(0, 'David', 'AUS'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable values(3, 'Chris', 'UK'); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable values(8, 'Carol', 'US'); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable values(9, 'Sam', 'US'); Query OK, 1 row affected (0.14 ... Read More

How to generate field in MySQL SELECT?

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

175 Views

Use keyword AS 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 (3.16 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Name) values('John'); Query OK, 1 row affected (0.50 sec) mysql> insert into DemoTable(Name) values('Robert'); Query OK, 1 row affected (0.29 sec) mysql> insert into DemoTable(Name) values('David'); Query OK, 1 row affected (0.54 sec)Display all records from the table using select statement −mysql> select *from DemoTable;Output+----+--------+ | Id | Name | ... Read More

Advertisements