Display MySQL Table Values Using Java

AmitDiwan
Updated on 11-Dec-2020 05:50:52

4K+ Views

For this, you can use the ResultSet concept. For connection, we will be using the MySQL JDBC Driver.Let us create a table −Examplemysql> create table demo87    -> (    -> name varchar(20),    -> age int    -> )    -> ; Query OK, 0 rows affected (0.62Insert some records into the table with the help of insert command −Examplemysql> insert into demo87 values('John', 21); Query OK, 1 row affected (0.15 mysql> insert into demo87 values('David', 23); Query OK, 1 row affected (0.12 mysql> insert into demo87 values('Bob', 22); Query OK, 1 row affected (0.16Display records from ... Read More

Check If Any Value is Null in a MySQL Table Single Row

AmitDiwan
Updated on 11-Dec-2020 05:47:37

604 Views

For this, you can use ISNULL in MySQL.Let us create a table −Examplemysql> create table demo86    -> (    -> value1 varchar(20)    -> ,    -> value2 varchar(20)    -> ); Query OK, 0 rows affected (2.77Insert some records into the table with the help of insert command −Examplemysql> insert into demo86 values(null, null); Query OK, 1 row affected (0.34 mysql> insert into demo86 values(null, 'John'); Query OK, 1 row affected (0.16 mysql> insert into demo86 values('David', 'Mike'); Query OK, 1 row affected (0.17 mysql> insert into demo86 values('Sam', null); Query OK, 1 row affected ... Read More

MySQL Sum Rows with Same ID

AmitDiwan
Updated on 11-Dec-2020 05:44:45

8K+ Views

To sum rows with same ID, use the GROUP BY HAVING clause.Let us create a table −Examplemysql> create table demo84    -> (    -> id int,    -> price int    -> )    -> ; Query OK, 0 rows affected (0.60Insert some records into the table with the help of insert command −Examplemysql> insert into demo84 values(1, 2000); Query OK, 1 row affected (0.08 mysql> insert into demo84 values(1, 2000); Query OK, 1 row affected (0.14 mysql> insert into demo84 values(2, 1800); Query OK, 1 row affected (0.14 mysql> insert into demo84 values(2, 2200); Query ... Read More

MySQL Query for INSERT INTO Using Values from Another Table

AmitDiwan
Updated on 11-Dec-2020 05:42:20

920 Views

For this, use INSERT INTO SELECT statement.Let us create a table −Examplemysql> create table demo82    -> (    -> id int,    -> name varchar(20)    -> ); Query OK, 0 rows affected (2.06Insert some records into the table with the help of insert command −Examplemysql> insert into demo82 values(100, 'John'); Query OK, 1 row affected (0.14 mysql> insert into demo82 values(101, 'Bob'); Query OK, 1 row affected (0.32 mysql> insert into demo82 values(101, 'David'); Query OK, 1 row affected (0.09 mysql> insert into demo82 values(101, 'Mike'); Query OK, 1 row affected (0.12 mysql> insert ... Read More

MySQL SELECT WHERE Values in List String

AmitDiwan
Updated on 11-Dec-2020 05:39:59

4K+ Views

For this, use FIND_IN_SET().Let us create a table −Examplemysql> create table demo81    -> (    -> id int not null auto_increment primary key,    -> username varchar(200)    -> ); Query OK, 0 rows affected (1.44Insert some records into the table with the help of insert command −Examplemysql> insert into demo81(username) values('John, Chris, David'); Query OK, 1 row affected (0.11 mysql> insert into demo81(username) values('Mike, Sam'); Query OK, 1 row affected (0.14 mysql> insert into demo81(username) values('Chris, Bob, Sam'); Query OK, 1 row affected (0.13 mysql> insert into demo81(username) values('Mike, John, Chris'); Query OK, 1 row ... Read More

Show Date Like 30-04-2020 Instead of 2020-04-30 from MySQL Database

AmitDiwan
Updated on 11-Dec-2020 05:38:01

88 Views

For this, use DATE_FORMAT() in MySQL. The syntax is as follows −Exampleselect date_format(yourColumnName, '%d-%m-%Y') as anyAliasName from yourTableName;Let us create a table −Examplemysql> create table demo80    -> (    -> due_date date    -> ); Query OK, 0 rows affected (0.74Insert some records into the table with the help of insert command −Examplemysql> insert into demo80 values('2020-04-30'); Query OK, 1 row affected (0.14 mysql> insert into demo80 values('2016-01-10'); Query OK, 1 row affected (0.17 mysql> insert into demo80 values('2018-03-21'); Query OK, 1 row affected (0.12Display records from the table using select statement −Examplemysql> select *from demo80;This will ... Read More

Split a Column in 2 Columns Using Comma as Separator in MySQL

AmitDiwan
Updated on 11-Dec-2020 05:35:52

4K+ Views

For this, you can use substring_index() in MySQL. Let us create a table −Examplemysql> create table demo79    -> (    -> fullname varchar(50)    -> ); Query OK, 0 rows affected (0.64Insert some records into the table with the help of insert command −Examplemysql> insert into demo79 values("John, Smith"); Query OK, 1 row affected (0.09 mysql> insert into demo79 values("David, Miller"); Query OK, 1 row affected (0.11 mysql> insert into demo79 values("Chris, Brown"); Query OK, 1 row affected (0.07Display records from the table using select statement −Examplemysql> select *from demo79;This will produce the following output −Output+--------------+ | ... Read More

Get Username Using ID from Another Table in MySQL Database

AmitDiwan
Updated on 11-Dec-2020 05:33:37

3K+ Views

To get username using ID from two tables, you need to use JOIN and join the tables.Let us create a table −Examplemysql> create table demo77    -> (    -> userid int not null primary key,    -> username varchar(20)    -> ); Query OK, 0 rows affected (2.63Insert some records into the table with the help of insert command −Examplemysql> insert into demo77 values(1, 'John'); Query OK, 1 row affected (0.19 mysql> insert into demo77 values(2, 'Bob'); Query OK, 1 row affected (0.36Display records from the table using select statement −Examplemysql> select *from demo77;This will produce the following ... Read More

Find All Users with a Unique Last Name in MySQL

AmitDiwan
Updated on 11-Dec-2020 05:30:40

562 Views

To find all users with unique last name, use GROUP BY HAVING COUNT().Let us create a table −Examplemysql> create table demo76    -> (    -> firstName varchar(20),    -> lastName varchar(20)    -> ); Query OK, 0 rows affected (9.29Insert some records into the table with the help of insert command −Examplemysql> insert into demo76 values('John', 'Doe'); Query OK, 1 row affected (2.52 mysql> insert into demo76 values('David', 'Smith'); Query OK, 1 row affected (6.31 mysql> insert into demo76 values('Adam', 'Smith'); Query OK, 1 row affected (1.52Display records from the table using select statement −Examplemysql> select *from ... Read More

Fetch Rows Updated at Timestamp Older Than 1 Day in MySQL

AmitDiwan
Updated on 11-Dec-2020 05:28:02

878 Views

For this, yYou can use from_unixtime() along with now().Let us create a table with some data type −Examplemysql> create table demo75    -> (    -> due_date int(11)    -> ); Query OK, 0 rows affected, 1 warning (2.87Insert some records into the table with the help of insert command −Examplemysql> insert into demo75 values(unix_timestamp("2020-01-10")); Query OK, 1 row affected (0.46 mysql> insert into demo75 values(unix_timestamp("2020-11-19")); Query OK, 1 row affected (0.59 mysql> insert into demo75 values(unix_timestamp("2020-12-18")); Query OK, 1 row affected (0.44 mysql> insert into demo75 values(unix_timestamp("2020-11-10")); Query OK, 1 row affected (0.70Display records from the ... Read More

Advertisements