Found 4378 Articles for MySQL

Difference between count(*) and count(columnName) in MySQL?

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

2K+ Views

The count(*) returns all rows whether column contains null value or not while count(columnName) returns the number of rows except null rows.Let us first create a table.Following is the querymysql> create table ifNotNullDemo    -> (    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.54 sec)Following is the query to insert some records in the table using insert command:mysql> insert into ifNotNullDemo values('Chris'); Query OK, 1 row affected (0.15 sec) mysql> insert into ifNotNullDemo values(''); Query OK, 1 row affected (0.13 sec) mysql> insert into ifNotNullDemo values('Robert'); Query OK, 1 row affected (0.24 sec) ... Read More

Resolve Unknown database in JDBC error with Java-MySQL?

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

8K+ Views

This type of error occurs if you select any database that does not exist in MySQL. Let us first display the error of unknown database in JDBC.The Java code is as follows. Here, we have set the database as ‘onlinebookstore’, which does not exist:import java.sql.Connection; import java.sql.DriverManager; public class UnknownDatabaseDemo {    public static void main(String[] args) {       String JdbcURL = "jdbc:mysql://localhost:3306/onlinebookstore?useSSL=false";       String Username = "root";       String password = "123456";       Connection con = null;       try {          con = DriverManager.getConnection(JdbcURL, Username, password); ... Read More

How do I INSERT INTO from one MySQL table into another table and set the value of one column?

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

283 Views

Let us first create a table. Following is the query −mysql> create table insertOneToAnotherTable    -> (    -> Value int    -> ); Query OK, 0 rows affected (0.60 sec)Following is the query to insert some records in the table using insert command −mysql> insert into insertOneToAnotherTable values(100); Query OK, 1 row affected (0.08 sec) mysql> insert into insertOneToAnotherTable values(200); Query OK, 1 row affected (0.15 sec) mysql> insert into insertOneToAnotherTable values(300); Query OK, 1 row affected (0.13 sec) mysql> insert into insertOneToAnotherTable values(400); Query OK, 1 row affected (0.15 sec) mysql> insert into insertOneToAnotherTable ... Read More

Select results from the middle of a sorted list in MySQL?

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

815 Views

To select results from the middle of a sorted list, use ORDER BY clause along with LIMIT.Let us first create a table. Following is the query −mysql> create table sortedListDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentName varchar(100)    -> ); Query OK, 0 rows affected (0.46 sec)Following is the query to insert some records in the table using insert command −mysql> insert into sortedListDemo(StudentName) values('John'); Query OK, 1 row affected (0.62 sec) mysql> insert into sortedListDemo(StudentName) values('Sam'); Query OK, 1 row affected (0.18 sec) mysql> insert into sortedListDemo(StudentName) values('Adam'); ... Read More

Reorder integer except for value 0 with MySQL?

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

55 Views

To reorder integer except for value 0, use the below syntax −select *from yourTableName order by yourColumnName=0 ,yourColumnName;Let us first create a table −mysql> create table reorderIntegerExcept0    -> (    -> value int    -> ); Query OK, 0 rows affected (0.70 sec)Following is the query to insert records in the table using insert command −mysql> insert into reorderIntegerExcept0 values(90); Query OK, 1 row affected (0.17 sec) mysql> insert into reorderIntegerExcept0 values(10); Query OK, 1 row affected (0.21 sec) mysql> insert into reorderIntegerExcept0 values(0); Query OK, 1 row affected (0.18 sec) mysql> insert into reorderIntegerExcept0 values(40); ... Read More

Insert current date to the database in MySQL?

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

4K+ Views

To insert current date to the database, you can use NOW(). Following is the syntax −INSERT INTO yourTableName(yourDateColumnName) VALUES(NOW());If your column has datatype date then NOW() function inserts only current date, not time and MySQL will give a warning. To remove the warning, you can use CURDATE().Let us first create a table −mysql> create table insertcurrentdate    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> currentDate date    -> ); Query OK, 0 rows affected (1.09 sec)Following is the query to insert some records in the table using insert command. We have used both NOW() ... Read More

MySQL format time with lowercase am/pm?

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

588 Views

To format MySQL time with lowercase am/pm, use the LOWER() as well as DATE_FORMAT().Let us first create a table −mysql> create table formatTime    -> (    -> LoginTime time    -> ); Query OK, 0 rows affected (0.56 sec)Following is the query to insert records in the table using insert command −mysql> insert into formatTime values('12:40:34'); Query OK, 1 row affected (0.20 sec) mysql> insert into formatTime values('14:10:58'); Query OK, 1 row affected (0.13 sec) mysql> insert into formatTime values('16:56:40'); Query OK, 1 row affected (0.18 sec) mysql> insert into formatTime values('10:12:14'); Query OK, 1 row ... Read More

Add10 minutes to MySQL datetime format?

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

306 Views

Use DATE_ADD() to add 10 minutes to datetime format. Following is the syntax −select date_add(yourColumnName ,interval 10 minute) from yourTableName;Let us first create a table −mysql> create table add10MinuteDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> DelayDatetime datetime    -> ); Query OK, 0 rows affected (0.83 sec)Following is the query to insert records in the table using insert command −mysql> insert into add10MinuteDemo(DelayDatetime) values('2019-01-23 12:45:56'); Query OK, 1 row affected (0.16 sec) mysql> insert into add10MinuteDemo(DelayDatetime) values('2019-03-25 10:30:23'); Query OK, 1 row affected (0.19 sec) mysql> insert into add10MinuteDemo(DelayDatetime) values('2019-04-21 04:04:30'); Query ... Read More

Can we GROUP BY one column and select all data in MySQL?

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

912 Views

Yes, you can use group_concat() for this. Let us first create a table −mysql> create table groupByOneSelectAll    -> (    -> StudentDetails varchar(100),    -> StudentName varchar(100)    -> ); Query OK, 0 rows affected (0.91 sec)Following is the query to insert some records in the table using insert command −mysql> insert into groupByOneSelectAll values('StudentFirstName', 'John'); Query OK, 1 row affected (0.14 sec) mysql> insert into groupByOneSelectAll values('StudentFirstName', 'Chris'); Query OK, 1 row affected (0.21 sec) mysql> insert into groupByOneSelectAll values('StudentFirstName', 'Robert'); Query OK, 1 row affected (0.65 sec) mysql> insert into groupByOneSelectAll values('StudentFirstName', 'Bob'); Query ... Read More

How to use the CAST function in a MySQL SELECT statement?

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

271 Views

The CAST() function in MySQL converts a value of any type into a value that has a specified type. Let us first create a table −mysql> create table castFunctionDemo    -> (    -> ShippingDate date    -> ); 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 castFunctionDemo values('2019-01-31'); Query OK, 1 row affected (0.20 sec) mysql> insert into castFunctionDemo values('2018-07-12'); Query OK, 1 row affected (0.16 sec) mysql> insert into castFunctionDemo values('2016-12-06'); Query OK, 1 row affected (0.16 sec) mysql> insert ... Read More

Advertisements