Found 4219 Articles for MySQLi

How to display two different sums of the same price from column Amount in MySQL?

AmitDiwan
Updated on 23-Dec-2019 12:01:23

105 Views

For this, you can use case statement. Let us first create a table −mysql> create table DemoTable1794      (      Amount int      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1794 values(100); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1794 values(80); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1794 values(320); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1794;This will produce the following output −+--------+ | Amount | ... Read More

Concatenate two columns when one of such column values is null in MySQL

AmitDiwan
Updated on 23-Dec-2019 12:00:01

1K+ Views

To avoid any issues while running a query, use IFNULL(). Let us first create a table −mysql> create table DemoTable1793      (      StudentFirstName varchar(20),      StudentLastName varchar(20)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1793 values('John', 'Smith'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1793 values('Carol', NULL); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1793 values(NULL, 'Brown'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from ... Read More

How do I select data that does not have a null record in MySQL?

AmitDiwan
Updated on 23-Dec-2019 11:58:10

63 Views

To select not-null records, use IS NOT NULL property. Let us first create a table −mysql> create table DemoTable1792      (      Name varchar(20)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1792 values('John Smith'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1792 values(NULL); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1792 values('David Miller'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1792 values(NULL); Query OK, 1 row affected (0.00 sec)Display all records from the table using ... Read More

What would happen if we run SELECT WHERE columnName = zero in MySQL?

AmitDiwan
Updated on 25-Feb-2020 13:21:35

69 Views

The following syntax will fetch all the values from the column −select * from yourTableName where yourColumnName=0;Let us first create a table −mysql> create table DemoTable1791      (      FirstName varchar(20)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1791 values('David'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1791 values('John'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1791 values('Carol'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1791;This ... Read More

Get multiple count in a single MySQL query for specific column values

AmitDiwan
Updated on 23-Dec-2019 11:54:48

597 Views

For this, you can use aggregate function sum() along with parameter value for specific column. Let us first create a table −mysql> create table DemoTable1790      (      Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,      Name varchar(20),      Score int      ); Query OK, 0 rows affected (0.94 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1790(Name, Score) values('Chris', 45); Query OK, 1 row affected (0.38 sec) mysql> insert into DemoTable1790(Name, Score) values('David', 55); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1790(Name, Score) values('David', 98); Query OK, ... Read More

How to treat MySQL longtext as integer in MySQL query?

AmitDiwan
Updated on 26-Feb-2020 05:02:08

217 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Value longtext    -> ); Query OK, 0 rows affected (0.94 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('778437437447488487476464644433334'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('9998888485775775757577578585'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('8888874757757757757757575675656'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('988757757574646'); Query OK, 1 row affected (0.09 sec)Display all records from the table using select statement −mysql> select * from DemoTable;This will produce the following ... Read More

How to insert only a single column into a MySQL table with Java?

AmitDiwan
Updated on 26-Feb-2020 05:03:06

476 Views

Use INSERT INTO statement in the Java-MySQL connection code to insert a column.Let us first create a table −mysql> create table DemoTable    -> (    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.54 sec)Here is the Java code to insert only a single column into a MySQL table.Exampleimport java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; public class InsertOneColumnDemo {    public static void main(String[] args) {       Connection con = null;       PreparedStatement ps = null;       try {          con = DriverManager.getConnection("jdbc:mysql://localhost:3306/web?" + "useSSL=false", "root", "123456");   ... Read More

How to fetch only a single result from a table in Java-MySQL?

AmitDiwan
Updated on 26-Feb-2020 05:04:00

2K+ Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Id int,    -> Name varchar(20)    -> ); Query OK, 0 rows affected (1.37 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(101, 'Chris'); Query OK, 1 row affected (0.54 sec) mysql> insert into DemoTable values(102, 'David'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(103, 'Mike'); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select * from DemoTable;This will produce the following output −+------+-------+ | Id   ... Read More

Sort character values from a column mixed with character and numbers in MySQL?

AmitDiwan
Updated on 26-Feb-2020 05:19:51

143 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Code varchar(20)    -> ); Query OK, 0 rows affected (0.70 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('J23'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('C13'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('A61'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('D56'); 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

Adding a new NOT NULL column to an existing table with records

AmitDiwan
Updated on 26-Feb-2020 05:20:44

178 Views

To add a new NOT NULL column to an already created table, use ALTER command. 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 (0.60 sec)Following is the query to add a new NOT NULL column to an existing table −mysql> alter table DemoTable add column StudentAge int NOT NULL; Query OK, 0 rows affected (0.52 sec) Records: 0 Duplicates: 0 Warnings: 0Insert some records in the table using insert command −mysql> insert into DemoTable(StudentName, ... Read More

Advertisements