Found 4219 Articles for MySQLi

MySQL case statement inside a select statement?

AmitDiwan
Updated on 12-Dec-2019 07:38:42

348 Views

For this, you can use CASE WHEN statement. Let us first create a table −mysql> create table DemoTable    -> (    -> FirstName varchar(20),    -> Score int -> ); Query OK, 0 rows affected (0.63 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John', 46); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('John', 78); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('John', 69); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Chris', 78); Query OK, 1 row affected (0.12 ... Read More

Order records and delete n rows in MySQL

AmitDiwan
Updated on 12-Dec-2019 07:35:13

101 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> FirstName varchar(20)    -> ); Query OK, 0 rows affected (0.65 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(FirstName) values('Chris'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(FirstName) values('Adam'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(FirstName) values('John'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(FirstName) values('David'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(FirstName) values('Mike'); Query OK, 1 ... Read More

Find the difference between two datetime values with MySQL?

AmitDiwan
Updated on 12-Dec-2019 07:29:24

320 Views

To find the difference between two datetime values, you can use TIMESTAMPDIFF(). Let us first create a table −mysql> create table DemoTable    -> (    -> DueDatetime1 datetime,    -> DueDatetime2 datetime    -> ); Query OK, 0 rows affected (0.86 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-10-26 19:49:00', '2019-10-26 17:49:00'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('2019-10-26 08:00:00', '2019-10-26 13:00:00'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('2019-10-26 06:50:00', '2019-10-26 12:50:00'); Query OK, 1 row affected (0.68 sec)Display all records from the ... Read More

Add a single day to datetime field with MySQL INTERVAL

AmitDiwan
Updated on 12-Dec-2019 07:27:21

113 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> DueDate date    ->); Query OK, 0 rows affected (2.11 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-01-21'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('2018-12-31'); Query OK, 1 row affected (0.47 sec) mysql> insert into DemoTable values('2018-12-30'); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable values('2017-02-26'); Query OK, 1 row affected (0.47 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+------------+ ... Read More

Shifting values of rows in MySQL to change the existing id values for existing rows?

AmitDiwan
Updated on 12-Dec-2019 07:25:47

343 Views

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

MySQL query to delete last two words from every column value

AmitDiwan
Updated on 12-Dec-2019 07:20:25

146 Views

For this, you can use the LEFT() function from MySQL. Let us first create a table −mysql> create table DemoTable    -> (    -> Name varchar(10)    -> ); Query OK, 0 rows affected (0.71 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.11 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+--------+ | ... Read More

Can we use the word user for a MySQL table?

AmitDiwan
Updated on 12-Dec-2019 07:17:57

191 Views

You cannot use “user” for a MySQL table because it is a reserved word in MySQL. You can change the name from user to users or something else or you can use backticks around the user word.The word user can be used to create a user or can select a user list from the MySQL database.Let us first create a table. Here, we have used the table name users −mysql> create table users    -> (    -> UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> UserName varchar(20),    -> UserAge int    -> ); Query OK, 0 rows ... Read More

MySQL Select where timestamp is in the current hour?

AmitDiwan
Updated on 12-Dec-2019 07:15:06

170 Views

For this, you can use CURTIME(). Let us first create a table −mysql> create table DemoTable    -> (    -> ArrivalTime timestamp    -> ); Query OK, 0 rows affected (0.65 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-10-26 17:55:55'); Query OK, 1 row affected (0.46 sec) mysql> insert into DemoTable values('2019-10-26 18:00:00'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('2019-10-26 18:55:00'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('2018-10-26 16:00:10'); Query OK, 1 row affected (0.20 sec)Display all records from the table using select ... Read More

How to declare a variable inside a procedure in MySQL?

AmitDiwan
Updated on 12-Dec-2019 07:13:15

1K+ Views

You can use the DECLARE command to declare a variable inside a MySQL procedure. Let us create a stored procedure in MySQL −mysql> DELIMITER // mysql> CREATE PROCEDURE DECLARE_VARIABLE_DEMO(IN value int)    -> BEGIN    -> DECLARE searchValue int;    -> set searchValue=value;    -> if searchValue=10 then    ->    select searchValue+100;    -> else    ->    select searchValue;    -> end if;    -> END    -> // Query OK, 0 rows affected (0.17 sec) mysql> DELIMITER ;Above, we have declared a variable. Now, let us call the stored procedure using CALL command −mysql> call DECLARE_VARIABLE_DEMO(10);This will ... Read More

Where we should close a connection in JDBC and MySQL?

AmitDiwan
Updated on 12-Dec-2019 07:11:39

1K+ Views

You need to close connection in finally block. Following is the Java code to close connection in JDBC and MySQL −import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class CloseConnectionDemoInFinallyBlock {    public static void main(String[] args) {       String JDBCURL = "jdbc:mysql://localhost:3306/web?useSSL=false";       Connection con = null;       try {          con = DriverManager.getConnection(JDBCURL, "root", "123456");          System.out.println("connection is open");       }       catch (Exception e) {          e.printStackTrace();       }       finally {          try {             con.close();          }          catch (SQLException sqlException) {             sqlException.printStackTrace();          }       }    } }This will produce the following output −connection is openHere is the screenshot of the output −

Advertisements