Found 4219 Articles for MySQLi

Get field value and convert a particular character from it to uppercase with MySQL

AmitDiwan
Updated on 27-Dec-2019 07:04:08

82 Views

Let us first create a table −mysql> create table DemoTable1897    (    Name varchar(20)    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1897 values('john'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1897 values('Chris'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1897 values('jace'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1897 values('David'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1897 values('Chris'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement ... Read More

Get only a single value from a specific MySQL row?

AmitDiwan
Updated on 27-Dec-2019 07:02:32

1K+ Views

For this, use SELECT INTO variable with where clause. Let us first create a table −mysql> create table DemoTable1896    (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentName varchar(20),    StudentMarks int    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1896(StudentName, StudentMarks) values('Chris', 56); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1896(StudentName, StudentMarks) values('David', 98); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1896(StudentName, StudentMarks) values('Mike', 89); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1896(StudentName, StudentMarks) values('Sam', ... Read More

What to assign to a MySQL column that must not be empty?

AmitDiwan
Updated on 27-Dec-2019 06:58:48

298 Views

Define with NOT NULL, if a column must not be empty. Let us first create a table with one of the columns as NOT NULL −mysql> create table DemoTable1895    (    Id int NOT NULL,    FirstName varchar(20),    LastName varchar(20) NOT NULL    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1895 values(100, 'John', 'Smith'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1895 values(NULL, 'Chris', 'Brown'); ERROR 1048 (23000): Column 'Id' cannot be null mysql> insert into DemoTable1895 values(102, 'Carol', NULL); ERROR 1048 (23000): ... Read More

Set a MySQL field with the current date (UNIX_TIMESTAMP(now))

AmitDiwan
Updated on 27-Dec-2019 06:57:19

486 Views

For this, use unix_timestamp(). Let us first create a table −mysql> create table DemoTable1894    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    DueTime int    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1894 values(); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1894 values(); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1894 values(); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1894;This will produce the following output −+----+---------+ | ... Read More

Java application to insert null value into a MySQL database?

AmitDiwan
Updated on 27-Dec-2019 06:55:33

785 Views

To set null value with Java, the statement is as follows −ps.setNull(yourIndex, Types.NULL);Let us first create a table −mysql> create table DemoTable1893    (    FirstName varchar(20)    ); Query OK, 0 rows affected (0.00 sec)The Java code is as follows −import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.Types; public class InsertNullValueIntoDatabase{    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");          String query="insert into DemoTable1893(FirstName) values(?) ";          ps= con.prepareStatement(query);   ... Read More

Can you allow a regex match in a MySQL Select statement?

AmitDiwan
Updated on 27-Dec-2019 06:51:28

103 Views

Yes, we can do regex match in a select statement −select yourColumnName from yourTableName where yourColumnName regexp '^yourValue';Let us first create a table −mysql> create table DemoTable1892    (    FirstName varchar(20)    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1892 values('John'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1892 values('Adam'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1892 values('Jace'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1892 values('Johny'); Query OK, 1 row affected (0.00 sec) mysql> insert into ... Read More

MySQL Stored procedure to declare two values and perform mathematical operation

AmitDiwan
Updated on 27-Dec-2019 06:50:21

624 Views

Let us first create a stored procedure −mysql> delimiter // mysql> create procedure declare_demo_sp()    begin    declare Value1 int;    declare Value2 int;    set Value1=100;    set Value2=2000;    select Value1,Value2,Value1*Value2 as MultiplicationResult;    end    // Query OK, 0 rows affected (0.00 sec) mysql> delimiter ;Call a stored procedure using CALL command −mysql> call declare_demo_sp();This will produce the following output −+--------+--------+----------------------+ | Value1 | Value2 | MultiplicationResult | +--------+--------+----------------------+ |    100 |   2000 |               200000 | +--------+--------+----------------------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.00 sec)

Check if MySQL entry exists and if it does, how to overwrite other columns?

AmitDiwan
Updated on 27-Dec-2019 06:48:48

205 Views

For this, use INSERT ON DUPLICATE KEY UPDATE command. Let us first create a table −mysql> create table DemoTable1891    (    FirstName varchar(20),    UNIQUE KEY(FirstName)    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1891 values('Chris') on duplicate key update  FirstName='Robert'; Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1891 values('David') on duplicate key update  FirstName='Robert'; Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1891 values('Chris') on duplicate key update  FirstName='Robert'; Query OK, 2 rows affected (0.00 sec)Display all records from the table ... Read More

Select data and set value to boolean based on timestamp column in MySQL

AmitDiwan
Updated on 27-Dec-2019 06:47:30

311 Views

For this, use IF(). Let us first see the current date −mysql> select curdate(); +------------+ | curdate()  | +------------+ | 2019-12-10 | +------------+ 1 row in set (0.00 sec)Let us first create a table −mysql> create table DemoTable1890    (    DueDate timestamp    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1890 values('2017-12-10'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1890 values('2021-12-10'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1890 values('2018-04-24'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1890 ... Read More

How to sum current month records in MySQL?

AmitDiwan
Updated on 27-Dec-2019 06:46:05

741 Views

To sum current month records, use the SUM() and MONTH() function. Let us first create a table −mysql> create table DemoTable1889    (    DueDate date,    Amount int    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1889 values('2019-12-11', 500); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1889 values('2019-11-11', 1000); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1889 values('2018-12-04', 700); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1889 values('2017-12-10', 300); Query OK, 1 row affected (0.00 sec)Display all records ... Read More

Advertisements