Found 4219 Articles for MySQLi

Selecting records 15 days before today in MySQL?

AmitDiwan
Updated on 26-Dec-2019 06:18:48

785 Views

For this, you can use the concept of INTERVAL and DATE_SUB(). Let us first create a table −mysql> create table DemoTable1845      (      ArrivalDate date      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1845 values('2019-12-02'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1845 values('2019-11-18'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1845 values('2019-12-18'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1845 values('2019-12-25'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1845 values('2019-11-15'); Query ... Read More

Calculating percentage in a MySQL query and round off the result

AmitDiwan
Updated on 26-Dec-2019 06:15:48

2K+ Views

For this, you can use CONCAT() and round(). Let us first create a table −mysql> create table DemoTable1844      (      Number int,      TotalNumber int      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1844 values(50, 500); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1844 values(80, 500); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1844 values(98, 500); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1844 values(45, 500); Query OK, 1 row affected (0.00 sec)Display all ... Read More

Get beginning and end date from a specific year in MySQL

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

393 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

376 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

100 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

180 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

394 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

178 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

365 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

623 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

Advertisements