Found 4381 Articles for MySQL

Compare date when the AdmissionDate is less than the current date in MySQL

AmitDiwan
Updated on 10-Oct-2019 11:41:28

331 Views

Let us first create a table −mysql> create table DemoTable (    AdmissionDate varchar(50) ); Query OK, 0 rows affected (0.63 sec)Note − Let’s say the current date is 14-Sep-2019.Insert some records in the table using insert command. Following is the query −mysql> insert into DemoTable values('15-Sep-2019'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('14-Sep-2019'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('13-Sep-2016'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('13-Sep-2019'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('13-Sep-2020'); Query OK, 1 row affected ... Read More

Can we implement nested insert with select in MySQL?

AmitDiwan
Updated on 10-Oct-2019 11:39:16

656 Views

Yes, we can implement nested insert with select in MySQL as shown in the below syntax −insert into yourTableName2(yourColumnName1, yourColumnName2, .....N) select yourColumnName1, yourColumnName2, ....N from yourTableName1 where yourCondition;Let us first see an example and create a table −mysql> create table DemoTable1 (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Name varchar(40) ); Query OK, 0 rows affected (0.88 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1(Name) values('Chris'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1(Name) values('David'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1(Name) values('Bob'); Query ... Read More

MySQL CASE WHEN with SELECT to display odd and even ids?

AmitDiwan
Updated on 10-Oct-2019 11:36:14

2K+ Views

Let us first create a table −mysql> create table DemoTable (    PageId int ); Query OK, 0 rows affected (0.85 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(233); Query OK, 1 row affected (0.36 sec) mysql> insert into DemoTable values(34); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(76); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(89); Query OK, 1 row affected (0.26 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+--------+ | PageId | +--------+ ... Read More

Perform Multi-table delete in MySQL

AmitDiwan
Updated on 10-Oct-2019 11:33:23

207 Views

For this, you can use DELETE command. Let us first create a table −mysql> create table DemoTable1 (    Id int,    Name varchar(20) ); Query OK, 0 rows affected (0.63 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values(1, 'Chris'); Query OK, 1 row affected (0.30 sec) mysql> insert into DemoTable1 values(2, 'David'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1 values(3, 'Bob'); Query OK, 1 row affected (0.20 sec)Display all records from the table using select statement −mysql> select *from DemoTable1;This will produce the following output −+------+-------+ | Id   ... Read More

Insert record in a MySQL table with Java

AmitDiwan
Updated on 17-Feb-2020 06:56:12

820 Views

Let us first create a table. Following is the query to create a table in MySQL −mysql> create table DemoTable(    Id int,    Name varchar(30),    CountryName varchar(30),    Age int ); Query OK, 0 rows affected (0.66 sec)Following is the Java code to access MySQL database −import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.Statement; public class AccessMySQLDatabase {    public static void main(String[] args) {       Connection con = null;       Statement st = null;       try {          con = DriverManager.getConnection("jdbc :mysql ://localhost :3306/web?" + "useSSL=false", "root", "123456"); ... Read More

MySQL query to perform selection using AND OR

AmitDiwan
Updated on 09-Oct-2019 12:45:57

110 Views

Let us first create a table −mysql> create table DemoTable (    StudentId int,    StudentName varchar(20),    StudentSubject varchar(20) ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10, 'Chris', 'MongoDB'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values(11, 'David', 'MySQL'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values(12, 'Bob', 'Java'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(10, 'Carol', 'MySQL'); Query OK, 1 row affected (0.51 sec) mysql> insert into DemoTable values(10, 'Chris', 'MongoDB'); Query ... Read More

MySQL Regex to match a pattern for ignoring a character in search like Chris.Brown?

AmitDiwan
Updated on 09-Oct-2019 12:42:07

182 Views

Let us first create a table −mysql> create table DemoTable (    Name varchar(40) ) ; Query OK, 0 rows affected (0.63 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John.Smith'); Query OK, 1 row affected (0.30 sec) mysql> insert into DemoTable values('Chris Brown'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Chris.Brown'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('David Miller'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values('Chris.Taylor'); Query OK, 1 row affected (0.18 sec)Display all records from the table using select ... Read More

MySQL query to group rows by the numeric characters in a string field?

AmitDiwan
Updated on 09-Oct-2019 12:39:43

171 Views

Fir this, you can concatenate 0 with string field with the help of + operator. The scenario here is like we need to fetch numeric “9844” from a string field “9844Bob”.Let us first create a table −mysql> create table DemoTable (    StudentId varchar(100) ); Query OK, 0 rows affected (0.92 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('9844Bob'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('6375DavidMiller'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('007'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable ... Read More

How to round MySQL column with float values and display the result in a new column?

AmitDiwan
Updated on 09-Oct-2019 12:36:12

532 Views

Let us first create a table −mysql> create table DemoTable (    Value float ); Query OK, 0 rows affected (0.95 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(12.4567); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(124.7884); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(45.64643); Query OK, 1 row affected (0.46 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+---------+ | Value | +---------+ | 12.4567 | | 124.788 | | 45.6464 | +---------+ ... Read More

Implement MySQL trigger in the first table to insert records in the second table?

AmitDiwan
Updated on 09-Oct-2019 12:34:23

225 Views

For this, the syntax is as follows −DELIMITER // create trigger yourTriggerName before insert on yourTableName1    for each row    begin          insert into yourTableName2 values (yourValue1, yourValue2, ...N); end ; // DELIMITER ;Let us first create a table −mysql> create table DemoTable1 (    StudentId int,    StudentName varchar(40) ); Query OK, 0 rows affected (0.69 sec)Here is the query to create second table −mysql> create table DemoTable2(    Id int,    Name varchar(40) ); Query OK, 0 rows affected (0.61 sec)Here is the query for trigger before insert −mysql> DELIMITER // mysql> create trigger ... Read More

Advertisements