Found 4219 Articles for MySQLi

MySQL Stored Procedure to create a table?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

3K+ Views

Following is the query to create a stored procedure that creates a table. Here, we are creating a table with three columns, one of them is Id −mysql> DELIMITER //    mysql> CREATE PROCEDURE Stored_Procedure_CreatingTable()    BEGIN       create table DemoTable       (       Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,       UserFirstName varchar(20),       UserLastName varchar(20)       );    END;    //    Query OK, 0 rows affected (0.16 sec) mysql> DELIMITER ;Now you can call stored procedure with the help of CALL command −mysql> call Stored_Procedure_CreatingTable(); ... Read More

Counting common elements in MySQL?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

340 Views

To count common elements, use COUNT() and GROUP BY. Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Number int    ); Query OK, 0 rows affected (0.20 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Number) values(10); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable(Number) values(20); Query OK, 1 row affected (0.04 sec) mysql> insert into DemoTable(Number) values(20); Query OK, 1 row affected (0.04 sec) mysql> insert into DemoTable(Number) values(30); Query OK, 1 row affected (0.04 sec) ... Read More

Counting presence of a NOT NULL value in MySQL

Anvi Jain
Updated on 30-Jul-2019 22:30:26

565 Views

To count presence of a NOT NULL value, use aggregate function COUNT(yourColumnName). Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    NumberOfQuestion int,    NumberOfSolution int    ); Query OK, 0 rows affected (0.20 sec)Insert some records in the table using insert command. Here, some of the values are NULL −mysql> insert into DemoTable(NumberOfQuestion, NumberOfSolution) values(20, 10); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable(NumberOfQuestion, NumberOfSolution) values(20, 2); Query OK, 1 row affected (0.04 sec) mysql> insert into DemoTable(NumberOfQuestion, NumberOfSolution) values(20, NULL); Query ... Read More

Select all duplicate MySQL rows based on one or two columns?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

424 Views

For this, use subquery along with HAVING clause. Let us first create a table −mysql> create table DemoTable    (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentFirstName varchar(20),    StudentLastName varchar(20)    ); Query OK, 0 rows affected (0.27 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(StudentFirstName, StudentLastName) values('John', 'Smith'); Query OK, 1 row affected (0.04 sec) mysql> insert into DemoTable(StudentFirstName, StudentLastName) values('Carol', 'Taylor'); Query OK, 1 row affected (0.04 sec) mysql> insert into DemoTable(StudentFirstName, StudentLastName) values('John', 'Doe'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(StudentFirstName, ... Read More

How to use CHAR_LENGTH() in MySQL CREATE TABLE query?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

78 Views

Use CHAR_LENGTH(yourColumnName) at the time of table creation. Let us first see an example and create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Title varchar(200),    `Number_of_characters` int as (char_length(Title))    ); Query OK, 0 rows affected (0.18 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Title) values('Introduction To MySQL'); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable(Title) values('Introduction To Java'); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable(Title) values('Introduction To MongoDB'); Query OK, 1 row affected (0.04 ... Read More

How to select only MySQL date from datetime column?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

396 Views

Use DATE_FORMAT for this. Let us first create a table −mysql> create table DemoTable    (    ShippingDate varchar(200)     ); Query OK, 0 rows affected (0.25 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('04:58 PM 10/31/2018'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('02:30 AM 01/01/2019'); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable values('12:01 AM 05/03/2019'); Query OK, 1 row affected (0.06 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+---------------------+ | ... Read More

Pull row with lowest number in a MySQL column?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

75 Views

Use aggregate function MIN() along with GROUP BY for this. Here, we will display the minimum ID for NumberOfProduct . Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    NumberOfProduct int    ); Query OK, 0 rows affected (0.19 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(NumberOfProduct) values(40); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(NumberOfProduct) values(40); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable(NumberOfProduct) values(60); Query OK, 1 row affected (0.07 sec) mysql> ... Read More

How to display data from a MySQL column separated by comma?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

517 Views

You can use GROUP_CONCAT() function from MySQL to display result as a comma separated list. Let us first create a table −mysql> create table DemoTable    (    Value int    ); Query OK, 0 rows affected (0.26 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable values(30); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable values(40); Query OK, 1 row affected (0.07 sec) mysql> ... Read More

How to sum based on field value in MySQL?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

303 Views

To sum based on field values, use aggregate function SUM() along with CASE statement. Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Price int,    isValidCustomer boolean,    FinalPrice int    ); Query OK, 0 rows affected (0.23 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Price, isValidCustomer, FinalPrice) values(20, false, 40); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable(Price, isValidCustomer, FinalPrice) values(45, true, 10); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(Price, isValidCustomer, FinalPrice) ... Read More

Apostrophe replacement in MySQL query?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

450 Views

To replace apostrophe, you can use replace(). Following is the syntax −update yourTableName set yourColumnName=replace(yourColumnName, '\'', '');Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Sentence varchar(100)    ); Query OK, 0 rows affected (0.17 sec)Insert some records in the table using insert command. Apostrophe is added for the sentence −mysql> insert into DemoTable(Sentence) values('Chris\'s Father'); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable(Sentence) values('Larry\'s Mother'); Query OK, 1 row affected (0.06 sec)Display all records from the table using select statement −mysql> select *from ... Read More

Advertisements