Found 4378 Articles for MySQL

How to order or choose rows in MySQL GROUP BY clause?

AmitDiwan
Updated on 16-Dec-2019 06:01:48

98 Views

Let us first create a table −mysql> create table DemoTable1572    -> (    -> StudentId int,    -> StudentMarks int,    -> StudentName varchar(20)    -> ); Query OK, 0 rows affected (0.56 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1572 values(1, 79, 'Sam'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1572 values(2, 89, 'Chris'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1572 values(3, 98, 'David'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable1572 values(4, 79, 'Bob'); Query OK, 1 row affected (0.10 sec)Display all ... Read More

What is the proper way to insert an IF statement into a MySQL query?

AmitDiwan
Updated on 16-Dec-2019 05:59:43

1K+ Views

To insert an IF statement into a MySQL query, use the below syntax::select yourColumnName ,if(yourCondition, yourStatement1, yourStatement2) from yourTableName;Let us first create a table −mysql> create table DemoTable1571    -> (    -> Id int,    -> Value int    -> ); Query OK, 0 rows affected (5.63 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1571 values(101, 500); Query OK, 1 row affected (1.07 sec) mysql> insert into DemoTable1571 values(102, 450); Query OK, 1 row affected (0.47 sec) mysql> insert into DemoTable1571 values(103, 300); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable1571 ... Read More

How to make a MySQL Connection in JAVA? What is the port number to set on locahost?

AmitDiwan
Updated on 16-Dec-2019 05:57:57

379 Views

You need to use port number 3306 in the URL. The syntax is as follows −jdbc:mysql://localhost:3306Exampleimport java.sql.Connection; import java.sql.DriverManager; public class MySQLConnectionToJava {    public static void main(String[] args) {       String JDBCURL="jdbc:mysql://localhost:3306/sample?useSSL=false";       Connection con=null;       try {          con = DriverManager.getConnection(JDBCURL,"root","123456");         if(con!=null) {             System.out.println("MySQL connection is successful with port 3306.");           }     }     catch(Exception e) {       e.printStackTrace();     } } }OutputMySQL connection is successful with port 3306.

MySQL query to check how to get time difference

AmitDiwan
Updated on 16-Dec-2019 05:54:15

210 Views

Let us first create a table −mysql> create table DemoTable1570    -> (    -> ArrivalTime datetime    -> ); Query OK, 0 rows affected (0.87 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1570 values('2019-10-15 5:10:00'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable1570 values('2019-10-15 23:00:00'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1570 values('2019-10-15 23:10:00'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1570 values('2019-10-15 16:10:00'); Query OK, 1 row affected (0.15 sec)Display all records from the table using select statement −mysql> select * from DemoTable1570;This ... Read More

MySQL insert a value to specific row and column

AmitDiwan
Updated on 13-Dec-2019 11:19:47

2K+ Views

Let us first create a table −mysql> create table DemoTable1569    -> (    -> StudentId varchar(10),    -> StudentName varchar(20)    -> ); Query OK, 0 rows affected (3.05 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1569 values('John_12', 'John Smith'); Query OK, 1 row affected (0.38 sec) mysql> insert into DemoTable1569 values('David_321', 'Carol Taylor'); Query OK, 1 row affected (0.50 sec) mysql> insert into DemoTable1569 values('David_989', 'David Miller'); Query OK, 1 row affected (0.24 sec)Display all records from the table using select statement −mysql> select * from DemoTable1569;This will produce the following output+-----------+--------------+ | ... Read More

Finding total number of rows of tables across multiple databases in MySQL?

AmitDiwan
Updated on 13-Dec-2019 11:18:29

260 Views

To fetch total number of table rows across databases, use aggregate function SUM() along with INFORMATION SCHEMA. Let us first create a table, which is in “web” database −mysql> create table DemoTable1568    -> (    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.61 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1568 values('Chris'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1568 values('Bob'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1568 values('David'); Query OK, 1 row affected (0.13 sec)Display all records from the table using select ... Read More

Getting the last 30 rows in MySQL

AmitDiwan
Updated on 13-Dec-2019 10:46:56

475 Views

To get the last 30 rows in MySQL, you need to use ORDER BY DESC and then LIMIT 30. The syntax is as follows −select * from yourTableName order by yourColumnName DESC LIMIT 30;Let us first create a table −mysql> create table DemoTable1567    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY    -> ); Query OK, 0 rows affected (0.82 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1567 values(), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), ... Read More

Using CASE statement in MySQL to display custom name for empty value

AmitDiwan
Updated on 13-Dec-2019 07:09:56

180 Views

For this, you can use CASE WHEN statement. Let us first create a table −mysql> create table DemoTable    -> (    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(''); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(''); Query OK, 1 row affected (0.11 sec)Display all records from the table using select statement −mysql> ... Read More

Implement ORDER BY in MySQL to order records in human readable format?

AmitDiwan
Updated on 13-Dec-2019 07:08:14

71 Views

For this, use INET_ATON() in MySQL. Let’s say our records are in the form of an IP Address. The INET_ATON() method would allow a user to convert IP Address records to the number and then we can use ORDER BY to order them.Let us first create a table −mysql> create table DemoTable    -> (    -> IpAddress varchar(50)    -> ); Query OK, 0 rows affected (1.36 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('192.168.110.78'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('192.168.110.87'); Query OK, 1 row affected (0.27 ... Read More

MySQL query to display custom text for empty columns

AmitDiwan
Updated on 13-Dec-2019 07:06:24

169 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> FirstName varchar(20)    -> ); Query OK, 0 rows affected (0.77 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(''); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Bob'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(''); Query OK, 1 row affected (0.13 sec)Display all records from the table ... Read More

Advertisements