Found 4219 Articles for MySQLi

Create a table inside a MySQL stored procedure and insert a record on calling the procedure

AmitDiwan
Updated on 04-Oct-2019 06:49:06

470 Views

Create a table inside the stored procedure and use INSERT as well −mysql> DELIMITER // mysql> CREATE PROCEDURE create_TableDemo(id int,name varchar(100),age int)    BEGIN    CREATE TABLE DemoTable    (       ClientId int NOT NULL,       ClientName varchar(30),       ClientAge int,       PRIMARY KEY(ClientId)    );    INSERT INTO DemoTable VALUES(id,name,age);    SELECT *FROM DemoTable;    END // Query OK, 0 rows affected (0.17 sec) mysql> DELIMITER ;Call the stored procedure using CALL command −mysql> CALL create_TableDemo(100,'Robert',28);This will produce the following output −+----------+------------+-----------+ | ClientId | ClientName | ClientAge | +----------+------------+-----------+ |      100 | Robert     |        28 | +----------+------------+-----------+ 1 row in set (0.76 sec) Query OK, 0 rows affected (0.78 sec)

How to find last date from records with date values in MySQL?

AmitDiwan
Updated on 04-Oct-2019 06:47:01

201 Views

To get the last date i.e. the latest, use aggregate function MAX() with a subquery. Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    ExpiryDate date ); Query OK, 0 rows affected (1.40 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(ExpiryDate) values('2018-12-31'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(ExpiryDate) values('2019-09-01'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(ExpiryDate) values('2019-09-01'); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable(ExpiryDate) values('2016-08-30'); Query OK, 1 row affected (0.13 ... Read More

Is it compulsory to set PRIMARY KEY for AUTO_INCREMENT value?

AmitDiwan
Updated on 04-Oct-2019 06:45:40

124 Views

Yes, use AUTO_INCREMENT with PRIMARY KEY. Let us first create a table −mysql> create table DemoTable (    EmployeeId int NOT NULL AUTO_INCREMENT,    EmployeeName varchar(40),    EmployeeAge int,    PRIMARY KEY(EmployeeId),    UNIQUE KEY(EmployeeName, EmployeeAge) ); Query OK, 0 rows affected (0.96 sec)Let us check the table description of the table −mysql> desc DemoTable;This will produce the following output −+--------------+-------------+------+-----+---------+----------------+ | Field        | Type        | Null | Key | Default | Extra          | +--------------+-------------+------+-----+---------+----------------+ | EmployeeId   | int(11)     | NO   | PRI | NULL   ... Read More

MySQL query to find the average of only first three values from a column with five values

AmitDiwan
Updated on 04-Oct-2019 06:42:16

175 Views

For this, you can use a subquery. Let us first create a table −mysql> create table DemoTable (    Score int ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(80); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(45); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(55); Query OK, 1 row affected (0.33 sec) mysql> insert into DemoTable values(78); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(88); Query OK, 1 row affected (0.13 sec)Display all records from ... Read More

MySQL query to display record with maximum count values in a group with other column values?

AmitDiwan
Updated on 04-Oct-2019 06:40:23

308 Views

For this, use the GROUP BY HAVING clause. Let us first create a table −mysql> create table DemoTable (    Value int ); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(88); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values(88); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(88); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(99); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(99); Query OK, 1 row affected (0.09 sec) mysql> insert ... Read More

How to count specific comma separated values in a row retrieved from MySQL database?

AmitDiwan
Updated on 04-Oct-2019 06:38:12

389 Views

To count comma-separated-values, use aggregate function COUNT(*) along with FIND_IN_SET(). Let us first create a table −mysql> create table DemoTable (    Value varchar(100) ); Query OK, 0 rows affected (0.49 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('10, 20, 60, 80'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('60, 70, 90'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('50, 55, 65, 60'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('90, 98, 97'); Query OK, 1 row affected (0.12 sec)Display all records ... Read More

Is there anything like substr_replace in MySQL?

AmitDiwan
Updated on 04-Oct-2019 06:36:38

129 Views

For this, use the INSERT() function from MySQL. The INSERT(str, pos, len, newstr) returns the string str, with the substring beginning at position pos and len characters long replaced by the string newstr. Returns the original string if pos is not within the length of the string.It replaces the rest of the string from position pos if len is not within the length of the rest of the string. Returns NULL if any argument is NULL.Let us first create a table −mysql> create table DemoTable (    Password varchar(50) ); Query OK, 0 rows affected (0.51 sec)Insert some records in ... Read More

Implement MySQL conditional GROUP BY with NOT IN to filter records from duplicate column values

AmitDiwan
Updated on 04-Oct-2019 06:34:01

85 Views

Let us first create a table −mysql> create table DemoTable (    Name varchar(40),    Score int ); Query OK, 0 rows affected (0.48 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Adam', 89); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Adam', 89); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Chris', 89); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Chris', 89); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Bob', 98); Query OK, 1 row affected (0.10 sec) mysql> insert ... Read More

Finding average marks of students for different subjects and display only the highest average marks in MySQL

AmitDiwan
Updated on 04-Oct-2019 06:32:27

852 Views

For this, you can use subquery. Let us first create a table −mysql> create table DemoTable (    StudentName varchar(40),    StudentMarks int ); Query OK, 0 rows affected (0.50 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Adam', 56); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Chris', 78); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Adam', 89); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Robert', 98); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('Chris', 65); Query OK, 1 ... Read More

MySQL query to generate row index (rank) in SELECT statement?

AmitDiwan
Updated on 06-Jul-2020 07:40:48

1K+ Views

To generate a row index, use ROW_NUMBER(). Let us first create a table −mysql> create table DemoTable (    Name varchar(40) ); Query OK, 0 rows affected (0.49 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.14 sec) mysql> insert into ... Read More

Advertisements