Found 4378 Articles for MySQL

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 −

Filtering data in a table for a required condition to eliminate a specific record with MySQL

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

49 Views

For this, you can use NOT LIKE operator. Let us first create a table −mysql> create table DemoTable    -> (    -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentName varchar(20),    -> StudentAdmissionYear varchar(20)    -> ); Query OK, 0 rows affected (1.22 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(StudentName, StudentAdmissionYear) values('Chris', '2017'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(StudentName, StudentAdmissionYear) values('David', '2015'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(StudentName, StudentAdmissionYear) values('Bob', '2019'); Query OK, 1 row affected (0.18 sec) mysql> insert ... Read More

MySQL query to find same value on multiple fields with LIKE operator?

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

455 Views

Let us first create a table −mysql> create table DemoTable    ->    -> (    -> StudentName varchar(20),    -> StudentSubject varchar(20)    -> ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John', 'MySQL'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Adam', 'MySQL'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values('John', 'Java'); Query OK, 1 row affected (0.34 sec) mysql> insert into DemoTable values('Carol', 'Java'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('John', 'MongoDB'); Query ... Read More

Fetch some words from the left in MySQL

AmitDiwan
Updated on 12-Dec-2019 06:59:37

84 Views

For this, use LEFT in MySQL. Let us first create a table −mysql> create table DemoTable    -> (    -> Title text    -> ); Query OK, 0 rows affected (0.59 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Java database connectivity to MySQL database'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Python with django framework'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('C with data structure and algorithm'); Query OK, 1 row affected (0.33 sec)Display all records from the table using select statement −mysql> select ... Read More

MySQL query not matching due to punctuation?

AmitDiwan
Updated on 12-Dec-2019 06:57:43

94 Views

Use the MySQL LIKE operator to match even when punctuation is present. Let us first create a table−mysql> create table DemoTable    -> (    -> Comments varchar(20)    -> ); Query OK, 0 rows affected (1.10 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Good, Morning'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Nice'); Query OK, 1 row affected (0.51 sec) mysql> insert into DemoTable values('good, bye!'); 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 ... Read More

Difference between intvalue='1' and intvalue=1 in MySQL?

AmitDiwan
Updated on 12-Dec-2019 06:58:34

40 Views

You need to use intvalue=1. The statement intvalue=’1’ is internally converted to cast(‘1’ as int) by MySQL.Let us first create a table −mysql> create table DemoTable1566    -> (    -> intvalue int    -> ); Query OK, 0 rows affected (0.64 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1566 values(1); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1566 values(55); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable1566 values(75); Query OK, 1 row affected (0.32 sec) mysql> insert into DemoTable1566 values(90); Query OK, 1 row affected (0.19 sec)Display all ... Read More

Advertisements