Found 6705 Articles for Database

Get beginning and end date from a specific year in MySQL

AmitDiwan
Updated on 26-Dec-2019 06:11:02

559 Views

For this, use MySQL YEAR() function. Let us first create a table −mysql> create table DemoTable1843      (      StartDate date,      EndDate date      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1843 values('2019-01-21', '2019-10-12'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1843 values('2018-10-12', '2018-12-31'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1843 values('2016-04-01', '2017-05-02'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1843;This will produce the ... Read More

Multiplying column with NULL row in MySQL?

AmitDiwan
Updated on 26-Dec-2019 06:04:14

487 Views

To multiply with NULL row, you can use COALESCE(). Let us first create a table −mysql> create table DemoTable1842      (      NumberOfItems int,      Amount int      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1842 values(10, 40); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1842 values(20, 5); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1842 values(NULL, 10); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1842;This ... Read More

Return records that do not have a value in a certain field with two SELECT statement in a single MySQL query

AmitDiwan
Updated on 26-Dec-2019 05:59:15

152 Views

For this, you can use WHERE clause along with subquery. Let us first create a table −mysql> create table DemoTable1840      (      UserName varchar(20),      UserType ENUM('GUEST', 'ADMIN')      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1840 values('Chris', 'Admin'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1840 values('David', 'Guest'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1840 values('Chris', 'Guest'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * ... Read More

What is the fastest way to insert a large number of rows into a MySQL table?

AmitDiwan
Updated on 26-Dec-2019 05:57:54

229 Views

The syntax for the fastest way is given below. Here, we have used INSERT INTO just once and formed an optimized way −insert into yourTableName values(NULL, yourValue1', yourValue2), (NULL, yourValue1', yourValue2), ....N;Let us first create a table −mysql> create table DemoTable1839      (      ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY,      ClientName varchar(20),      ClientAge int      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1839 values(NULL, 'Chris', 29), (NULL, 'Chris', 29), (NULL, 'Chris', 29), (NULL, 'Chris', 29), (NULL, 'Chris', 29), (NULL, 'Chris', ... Read More

How to connect Java to MySQL?

AmitDiwan
Updated on 24-Dec-2019 07:58:15

521 Views

To connect Java to MySQL, the Java code is as follows −import java.sql.Connection; import java.sql.DriverManager; public class LostConnectionURLDemo {    public static void main(String[] args){       String JDBCURL="jdbc:mysql://localhost:3306/web?autoReconnect=true";       Connection con=null;        try{           con = DriverManager.getConnection(JDBCURL,"root","123456");           System.out.println("connection is established");        }       catch(Exception e){          e.printStackTrace();       }    } }This will produce the following output −

Sort search results based on substring position in MySQL

AmitDiwan
Updated on 24-Dec-2019 07:55:43

223 Views

To sort search results based on substring position, use ORDER BY LOCATE(). Let us first create a table −mysql> create table DemoTable1838      (      Subject varchar(100)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1838 values('MongoDB MySQL'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1838 values('MySQL Java'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1838 values('JavaWithMySQL'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1838; This will ... Read More

How to get a specific column record from SELECT query in MySQL?

AmitDiwan
Updated on 24-Dec-2019 07:54:20

486 Views

Let us first create a table −mysql> create table DemoTable1837      (      StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,      StudentName varchar(20)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1837(StudentName) values('Chris'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1837(StudentName) values('David'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1837(StudentName) values('Bob'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1837(StudentName) values('Mike'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement ... Read More

Fix MySQL Error #1064 - You have an error in your SQL syntax… near 'TYPE=MyISAM?

AmitDiwan
Updated on 24-Dec-2019 07:52:21

798 Views

This error occurs when we use TYPE for ENGINE NAME. The error is as follows −mysql> create table DemoTable1836      (      ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY,      ClientName varchar(20)      )Type=MyISAM AUTO_INCREMENT=1; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Type=MyISAM AUTO_INCREMENT=1' at line 5Now, in MySQL 8, you can use ENGINE instead of Type. Let us first create a table −mysql> create table DemoTable1836      (      ClientId int NOT ... Read More

How to fetch random rows in MySQL with comma separated values?

AmitDiwan
Updated on 24-Dec-2019 07:51:07

208 Views

To fetch random rows in MySQL, use ORDER BY RAND(). Let us first create a table −mysql> create table DemoTable1835      (      ListOfIds varchar(20)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1835 values('10, 20, 30'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1835 values('70, 80, 90'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1835 values('45, 67, 89'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1835 values('98, 96, 49'); Query OK, 1 row affected (0.00 ... Read More

Select rows containing a string in a specific column with MATCH and AGAINST in MySQL

AmitDiwan
Updated on 24-Dec-2019 07:49:58

238 Views

Let us first create a table −mysql> create table DemoTable1833      (      Name varchar(20)      ); Query OK, 0 rows affected (0.00 sec)Alter table −Mysql> alter table DemoTable1833 ADD FULLTEXT(Name); Query OK, 0 rows affected, 1 warning (0.00 sec) Records: 0  Duplicates: 0  Warnings: 1Insert some records in the table using insert command −mysql> insert into DemoTable1833 values('John Doe'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1833 values('Adam Smith'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1833 values('Chris Brown'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1833 ... Read More

Advertisements