Found 4219 Articles for MySQLi

Insertion in a MySQL table with only a single column set as auto_increment?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

87 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> StudentId int NOT NULL AUTO_INCREMENT,    -> PRIMARY KEY(StudentId)    -> ); Query OK, 0 rows affected (0.60 sec)Insert some records in the table using insert command. Here, we have inserted a value, but since it is AUTO_INCREMENT, therefore, the default value would be visible −mysql> insert into DemoTable(StudentId) values(0); Query OK, 1 row affected (0.18 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output. The default AUTO_INCREMENT value 1 as the first value is visible ... Read More

Execute operations (plus, minus, multiply, divide) while updating a MySQL table?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

300 Views

Following is the syntax executing the plus (+) operator −update yourTableName set yourColumnName3=(yourColumnName1+yourColumnName2)The above syntax is only for plus operator. You need to change symbol like -, *, / for other operations. Let us first create a table −mysql> create table DemoTable    -> (    -> Number1 int,    -> Number2 int,    -> AddResult int,    -> MinusResult int,    -> MultiplyResult int,    -> DivideResult int    -> ); Query OK, 0 rows affected (0.89 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Number1, Number2) values(40, 20); Query OK, 1 row affected (0.16 ... Read More

MySQL query to get the current date from the list of dates

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

95 Views

For the current day, you can use CURDATE() method. Let us first create a table −mysql> create table DemoTable    -> (    -> DueDate date    -> ); Query OK, 0 rows affected (1.35 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-06-14' ); Query OK, 1 row affected (0.35 sec) mysql> insert into DemoTable values('2019-06-15'); Query OK, 1 row affected (0.64 sec) mysql> insert into DemoTable values('2019-06-16'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('2018-01-16'); Query OK, 1 row affected (0.29 sec) mysql> insert into ... Read More

Counting the number of non-null or nonzero columns in a MySQL table?

karthikeya Boyini
Updated on 30-Jun-2020 09:45:00

501 Views

Use if() method for this. Let us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Number1 int,    -> Number2 int    -> ); Query OK, 0 rows affected (1.15 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Number1, Number2) values(10, 20); Query OK, 1 row affected (0.41 sec) mysql> insert into DemoTable(Number1, Number2) values(0, 32); Query OK, 1 row affected (0.38 sec) mysql> insert into DemoTable(Number1, Number2) values(40, 0); Query OK, 1 row affected (0.21 sec) mysql> insert ... Read More

Set a certain value first with MySQL ORDER BY?

Sharon Christine
Updated on 30-Jun-2020 09:47:00

1K+ Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Number int    -> ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(12); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values(14); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(19); Query OK, 1 row affected (0.14 sec) ... Read More

How to select month and year from dates in MySQL?

Sharon Christine
Updated on 30-Jun-2020 09:49:03

392 Views

To select month and year in MySQL, you can use MONTH() and YEAR() method. Let us first create a table −mysql> create table DemoTable    -> (    -> DueDate datetime    -> ); Query OK, 0 rows affected (0.90 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-01-21'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values('2019-06-15'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('2018-12-01'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('2015-04-01'); Query OK, 1 row affected (0.16 sec) ... Read More

How to extract the digit part from the string in MySQL?

Sharon Christine
Updated on 30-Jun-2020 09:50:34

2K+ Views

Let us first create a table −mysql> create table DemoTable    -> (    -> StudentId varchar(100)    -> ); Query OK, 0 rows affected (0.71 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John19383'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('Carol9999'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('David123456'); Query OK, 1 row affected (0.15 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+-------------+ | StudentId   | +-------------+ | John19383 ... Read More

Implement multiple COUNT() in a single MySQL query

karthikeya Boyini
Updated on 30-Jun-2020 09:31:00

671 Views

For this, use CASE statement. Let us first create a table −mysql> create table DemoTable    -> (    -> LastName varchar(100)    -> ); Query OK, 0 rows affected (0.60 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Smith'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Brown'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Brown'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Smith'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Taylor'); Query OK, 1 ... Read More

Can we use PRIMARY KEY( column1, column2) in MySQL to make pairs?

karthikeya Boyini
Updated on 30-Jun-2020 09:33:04

120 Views

Yes, you can use below syntax. Following is the syntax −PRIMARY KEY(yourColumnName1, yourColumnName2);Let us first create a table −mysql> create table DemoTable    -> (    -> StudentFirstName varchar(100),    -> StudentLastName varchar(100),    -> StudentAge int,    -> StudentCountryName varchar(100),    -> PRIMARY KEY(StudentFirstName, StudentLastName)    -> ); Query OK, 0 rows affected (0.74 sec)Let us check the description of the table −mysql> desc DemoTable;OutputThis will produce the following output −+--------------------+--------------+------+-----+---------+-------+ | Field              | Type         | Null | Key | Default | Extra | +--------------------+--------------+------+-----+---------+-------+ | StudentFirstName   | ... Read More

Can we use semicolon as a MySQL DEMILITER?

karthikeya Boyini
Updated on 30-Jun-2020 09:33:49

87 Views

No, we cannot. If you still did it, then stored procedure won’t get created. Therefore, first you need to change your DELIMITER from semicolon(;) to others like (// ,??..etc). Following is the syntax −DELIMITER // CREATE PROCEDURE yourProcedureName() BEGIN yourStatement1, . . . . N END // DELIMITER ;Let us implement the above syntax in order to create a stored procedure −mysql> DELIMITER // mysql> CREATE PROCEDURE get_Message()    -> BEGIN    -> SELECT CONCAT("HELLO", " ", "MYSQL USERS");    -> END    -> // Query OK, 0 rows affected (0.23 sec) mysql> DELIMITER ;Now you can call the stored ... Read More

Advertisements