Found 6705 Articles for Database

Add some months to current date using Java with MySQL?

AmitDiwan
Updated on 19-Nov-2020 11:15:54

294 Views

Following is the syntax to add months using INTERVAL with Java − MySQL.String query; query = "insert into yourTableName values(curdate()+interval howManyNumberOfMonths month)";Following is the current date −mysql> select curdate(); +------------+ | curdate() | +------------+ | 2020-10-25 | +------------+ 1 row in set (0.00 sec)Let us create a table −mysql> create table demo12 −> ( −> created_date date −> ); Query OK, 0 rows affected (1.84 sec)Here, I am going to add 2 months to the current date from Java. Now, date will be inserted into the table 2020−12−25.The Java code is as followsExampleimport java.sql.Connection; import java.sql.DriverManager; import com.mysql.jdbc.Statement; public ... Read More

How to get substring results from a table with file location recordsi in MySQL?

AmitDiwan
Updated on 19-Nov-2020 11:11:23

183 Views

To fetch sub strings, use the substr() method in MySQL as in the below syntax −select substr(yourColumnName, startIndex, endIndex) from yourTableName limit anyValue; select substr(yourColumnName, startIndex+endIndex) from yourTableName limit anyValue;Let us create a table −mysql> create table demo11 −> ( −> id int NOT NULL AUTO_INCREMENT PRIMARY KEY, −> fileLocation text −> ); Query OK, 0 rows affected (2.60 sec)Insert some records into the table with the help of insert command −mysql> insert into demo11(fileLocation) values('E:/users/program/sample.sql'); Query OK, 1 row affected (0.15 sec) mysql> insert into demo11(fileLocation) values('E:/users/data/db.sql'); Query OK, 1 row affected (0.32 sec) mysql> insert ... Read More

Is there a way to make a list from a MySQL table in Java?

AmitDiwan
Updated on 19-Nov-2020 11:07:20

2K+ Views

Yes, for this, use the concept of ArrayList in Java. The syntax is as follows −ArrayList anyVariableName= new ArrayList();Let us create a table −mysql> create table demo10 −> ( −> id int not null auto_increment primary key, −> name varchar(20) −> ); Query OK, 0 rows affected (2.19 sec)Insert some records into the table with the help of insert command −mysql> insert into demo10(name) values('John'); Query OK, 1 row affected (0.23 sec) mysql> insert into demo10(name) values('Bob'); Query OK, 1 row affected (0.12 sec) mysql> insert into demo10(name) values('David'); Query OK, 1 row affected (0.13 sec)Display records from ... Read More

How to merge MySQL results?

AmitDiwan
Updated on 19-Nov-2020 11:01:32

372 Views

To merge, use simple join. Following is the syntax −select aliasName1.yourColumnName1, aliasName1.yourColumnName2, . . .N aliasName2.yourColumnName1 from yourTableName1 aliasName1 . . . N join yourTableName2 aliasName2 on yourCondition;Let us create a table −mysql> create table demo8 −> ( −> id int, −> name varchar(20) −> ); Query OK, 0 rows affected (1.77 sec)Insert some records into the table with the help of insert command −mysql> insert into demo8 values(100, 'John'); Query OK, 1 row affected (0.09 sec) mysql> insert into demo8 values(101, 'Mike'); Query OK, 1 row affected (0.16 sec) mysql> insert into demo8 values(102, 'Bob'); Query OK, ... Read More

Group result in MySQL and show on list?

AmitDiwan
Updated on 19-Nov-2020 10:58:57

139 Views

For this, use GROUP BY along with ORDER BY −select yourColumnName, count(*) as anyAliasName from yourTableName group by yourColumnName order by yourColumnName;Let us create a table −mysql> create table demo7 −> ( −> id int NOT NULL AUTO_INCREMENT, −> first_name varchar(50) −> , −> primary key(id) −> ); Query OK, 0 rows affected (1.22 sec)Insert some records into the table with the help of insert command −mysql> insert into demo7(first_name) values('John'); Query OK, 1 row affected (0.09 sec) mysql> insert into demo7(first_name) values('David'); Query OK, 1 row affected (0.22 sec) mysql> insert into demo7(first_name) values('John'); Query OK, 1 row affected ... Read More

MySQL Error ERROR 1099 (HY000): Table was locked with a READ lock and can't be updated

AmitDiwan
Updated on 19-Nov-2020 10:54:59

716 Views

To get rid of LOCK TABLES query, you need to use UNLOCK TABLES.Let us create a table −mysql> create table demo6 −> ( −> country_name varchar(100 −> ) −> ); Query OK, 0 rows affected (1.51 sec)Insert some records into the table with the help of insert command −mysql> insert into demo6 values('US'); Query OK, 1 row affected (0.11 sec) mysql> insert into demo6 values('UK'); Query OK, 1 row affected (0.08 sec) mysql> insert into demo6 values('AUS'); Query OK, 1 row affected (0.11 sec)Display records from the table using select statement −mysql> select *from demo6;This will produce the ... Read More

Limit total number of results across tables in MySQL?

AmitDiwan
Updated on 19-Nov-2020 10:48:20

189 Views

For this, you can use UNION ALL along with LIMIT concept. For our example, we will create three tables.Let us create the first table −mysql> create table demo3 −> ( −> value int −> ); Query OK, 0 rows affected (1.39 sec)Insert some records into the table with the help of insert command −mysql> insert into demo3 values(10); Query OK, 1 row affected (0.13 sec) mysql> insert into demo3 values(20); Query OK, 1 row affected (0.08 sec) mysql> insert into demo3 values(30); Query OK, 1 row affected (0.08 sec)Display records from the table using select statement −mysql> select ... Read More

Multiple LIKE Operators with ORDER BY in MySQL?

AmitDiwan
Updated on 19-Nov-2020 10:40:51

241 Views

Following is the syntax implementing multiple LIKE operators with ORDER BY −select *from yourTableName order by (    yourColumnName like '%yourValue1%' ) + (    yourColumnName like '%yourValue2%' ) + . . N desc;Let us create a table −mysql> create table demo2 −> ( −> id int not null auto_increment, −> name varchar(100), −> primary key(id) −> ); Query OK, 0 rows affected (1.53 sec)Insert some records into the table with the help of insert command −mysql> insert into demo2(name) values('John'); Query OK, 1 row affected (0.18 sec) mysql> insert into demo2(name) values('David'); Query OK, 1 row affected (0.09 ... Read More

How to get number located at 2 places before decimal point MySQL?

AmitDiwan
Updated on 19-Nov-2020 10:37:16

201 Views

In order to get number located at 2 places before decimal point, you can use the concept of div.Let us create a table −mysql> create table demo1 −> ( −> value float −> ); Query OK, 0 rows affected (2.20 sec)Insert some records into the table with the help of insert command −mysql> insert into demo1 values(456.54); Query OK, 1 row affected (0.16 sec) mysql> insert into demo1 values(50.64); Query OK, 1 row affected (0.17 sec) mysql> insert into demo1 values(1000.78); Query OK, 1 row affected (0.13 sec)Display records from the table using select statement −mysql> select *from demo1;This will ... Read More

What is the usage of scrollable cursor for current positioning?

Mandalika
Updated on 15-Sep-2020 11:14:12

212 Views

We can use SCROLLABLE CURSOR to directly point the cursor to the mentioned relative position. The relative position is the position of the row in the result table from the current row. For example, consider the table below.ORDER_IDORDER_DATEA223672020-07-28A667562020-07-28A778902020-07-29A968322020-07-29If the cursor is currently pointing to the 2nd absolute row i.e, ORDER_ID A66756 then the relative +2 position will be ORDER_ID A96832 and relative -1 position will be ORDER_ID A22367.The syntax to use relative position in FETCH statement is−EXEC SQL    FETCH RELATIVE +2 ORDER_CURR    INTO :ORDER-ID, :ORDER-DATE END-SQLRead More

Advertisements