Found 4219 Articles for MySQLi

Using the value of an alias inside the same MySQL SELECT statement

AmitDiwan
Updated on 07-Oct-2019 12:37:47

634 Views

You cannot directly use an alias in the SELECT. Instead, use a user-defined variable. Following is the syntax. Here, @yourAliasName is our variable and alias −select @yourAliasName :=curdate() as anyAliasName, concat(‘yourValue.', yourColumnName, ' yourValue', @yourAliasName) as anyAliasName from yourTableName;Let us first create a table −mysql> create table DemoTable (    Name varchar(40) ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John Smith'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('Chris Brown'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('David Miller'); ... Read More

Add some value to an int type column value in a table without knowing its current value in SQL?

AmitDiwan
Updated on 07-Oct-2019 12:35:18

109 Views

For this, simply use UPDATE command along with SET. Let us first create a table −mysql> create table DemoTable (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentScore int ); Query OK, 0 rows affected (0.81 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(StudentScore) values(78); Query OK, 1 row affected (0.46 sec) mysql> insert into DemoTable(StudentScore) values(89); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(StudentScore) values(67); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(StudentScore) values(95); Query OK, 1 row affected (0.25 sec)Display all records from the table ... Read More

Change date format in MySQL database table to d/m/y

AmitDiwan
Updated on 07-Oct-2019 12:31:43

303 Views

Following is the syntax −select date_format(yourColumnName, '%d/%m/%Y') as anyAliasName from yourTableName;Let us first create a table −mysql> create table DemoTable (    AdmissionDate date ); Query OK, 0 rows affected (0.89 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-01-21'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('2019-09-09'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('2018-12-31'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('2017-11-01'); Query OK, 1 row affected (0.16 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This ... Read More

MySQL query to check if multiple rows exist?

AmitDiwan
Updated on 07-Oct-2019 12:29:39

632 Views

Let us first create a table −mysql> create table DemoTable1219 (    Id int,    Name varchar(40) ); Query OK, 0 rows affected (0.43 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1219 values(100, 'Adam'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1219 values(101, 'John'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable1219 values(102, 'Chris'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1219 values(103, 'Bob'); Query OK, 1 row affected (0.18 sec)Display all records from the table using select statement −mysql> select *from DemoTable1219;This will produce the ... Read More

How to insert DATE into a MySQL column value using Java?

AmitDiwan
Updated on 14-Feb-2020 10:23:16

2K+ Views

For this, you can use PreparedStatement from Java. Let us first create a table wherein one of the columns is ArrivalDate with DATE type −mysql> create table DemoTable(    PassengerId int,    PassengerName varchar(40),    ArrivalDate date ); Query OK, 0 rows affected (0.82 sec)The JAVA code is as follows to insert date −import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; public class InsertDateFromJava {    public static void main(String[] args) {       Connection con = null;       PreparedStatement ps = null;       try {          java.util.Date javaDate = new java.util.Date();     ... Read More

Increment multiple Timestamp values by setting the incremented value in a user-defined variable in SQL

AmitDiwan
Updated on 07-Oct-2019 12:20:14

220 Views

The incremented value can be set in a user-defined variable as shown below. Here, “yourValue” is the incremented value. After that, use MySQL UPDATE to update the column and increment timestamp values −set @anyVariableName :=yourValue; update yourTableName set yourColumnName=yourColumnName+interval (@yourVariableName) second;Let us first create a table −mysql> create table DemoTable (    DueDatetime timestamp ); Query OK, 0 rows affected (0.73 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-01-31 12 :30 :40'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable values('2019-09-06 10 :00 :00'); Query OK, 1 row affected (0.73 sec) ... Read More

Counting different distinct items in a single MySQL query?

AmitDiwan
Updated on 07-Oct-2019 12:15:48

95 Views

To count items, use COUNT() along with DISTINCT. Here, DISTINCT is used to return distinct values. Let us now see an example and create a table −mysql> create table DemoTable (    CustomerId int,    CustomerName varchar(20),    ProductName varchar(40) ); Query OK, 0 rows affected (1.02 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(101, 'Chris', 'Product-1'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(102, 'David', 'Product-2'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(101, 'Chris', 'Product-1'); Query OK, 1 row affected (0.30 sec) mysql> insert ... Read More

How to ORDER BY grouped fields in MySQL?

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

66 Views

To ORDER BY grouped fields, use ORDER BY CASE along with IN(). CASE evaluates different conditions whereas ORDER BY sort values in ascending or descending order. The MySQL IN() is used to find a match.Let us first create a table −mysql> create table DemoTable (    Value varchar(40) ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values('100&101'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values('Sam'); Query OK, 1 row affected (0.09 sec) ... Read More

How to check if value exists with MySQL SELECT 1?

AmitDiwan
Updated on 07-Oct-2019 12:06:47

266 Views

Use SELECT 1 for this as in the below syntax −select 1 from yourTableName where yourColumnName=yourValue;If the above returns 1, that means value exists in the MySQL database. Let us first see an example and create a table −mysql> create table DemoTable (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentName varchar(40),    StudentAge int ); Query OK, 0 rows affected (0.46 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(StudentName, StudentAge) values('Chris', 21); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(StudentName, StudentAge) values('David', 20); Query OK, 1 row affected (0.16 ... Read More

MySQL CASE statement to place custom values in place of NULL

AmitDiwan
Updated on 07-Oct-2019 12:04:05

145 Views

Let us first create a table −mysql> create table DemoTable (    FirstName varchar(20) ); Query OK, 0 rows affected (1.15 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(NULL); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Adam'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(NULL); 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 output −+-----------+ | FirstName | +-----------+ ... Read More

Advertisements