Found 4219 Articles for MySQLi

How to write a procedure to insert data in the table in phpMyAdmin?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

373 Views

Let us first create a new table and understand the concept in continuationmysql> create table StoredProcedureInsertDemo    -> (    -> UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> UserName varchar(20),    -> UserAge int    -> ); Query OK, 0 rows affected (0.63 sec)Here is the query to create a stored procedure to insert data in to the tablemysql> DELIMITER // mysql> create procedure procedure_InsertIntoTable(IN FirstName VARCHAR(100), IN Age INT)    -> BEGIN    -> insert into StoredProcedureInsertDemo(UserName, UserAge) values (FirstName, Age);    -> END    -> // Query OK, 0 rows affected (0.34 sec) mysql> DELIMITER ;Call ... Read More

How to retrieve a random row or multiple random rows in MySQL?

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

150 Views

You can use RAND() method for this. To retrieve a random row, use the following syntaxSELECT *FROM yourTableName ORDER BY RAND() LIMIT yourIntegerNumber;To understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table gettingRandomRow    -> (    -> CustomerId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> CustomerName varchar(100)    -> ); Query OK, 0 rows affected (0.45 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into gettingRandomRow(CustomerName) values('Chris'); Query OK, 1 row affected (0.14 sec) mysql> insert into gettingRandomRow(CustomerName) values('Robert'); ... Read More

MySQL query to GROUP BY multiple columns

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

646 Views

You can use IF() to GROUP BY multiple columns. To understand the concept, let us create a table. The query to create a table is as followsmysql> create table MultipleGroupByDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> CustomerId int,    -> ProductName varchar(100)    -> ); Query OK, 0 rows affected (0.59 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into MultipleGroupByDemo(CustomerId, ProductName) values(1000, 'Product-1'); Query OK, 1 row affected (0.20 sec) mysql> insert into MultipleGroupByDemo(CustomerId, ProductName) values(1001, 'Product-2'); Query OK, 1 row affected (0.18 ... Read More

How can I sum columns across multiple tables in MySQL?

George John
Updated on 30-Jul-2019 22:30:25

1K+ Views

To sum columns across multiple tables, use UNION ALL. To understand the concept, let us create first table. The query to create first table is as followsmysql> create table Products1    -> (    -> ProductId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> ProductName varchar(20),    -> ProductPrice int    -> ); Query OK, 0 rows affected (0.50 sec)Insert some records in the first table using insert command. The query is as follows −mysql> insert into Products1(ProductName, ProductPrice) values('Product-1', 100); Query OK, 1 row affected (0.22 sec) mysql> insert into Products1(ProductName, ProductPrice) values('Product-2', 200); Query OK, 1 row affected ... Read More

MySQL replication: temporarily prevent specific SQL statements replicating to the slaves?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

88 Views

To achieve this, you need to set sql_log_bin to 0. To understand the concept, let us create a table. The query to create a table is as followsmysql> create table SQLStatementsDemo    -> (    -> UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> UserName varchar(20)    -> ); Query OK, 0 rows affected (0.79 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into SQLStatementsDemo(UserName) values('John'); Query OK, 1 row affected (0.18 sec) mysql> insert into SQLStatementsDemo(UserName) values('Carol'); Query OK, 1 row affected (0.14 sec) mysql> insert into SQLStatementsDemo(UserName) values('Bob'); Query ... Read More

Does deleting row from view delete row from base table in MySQL?

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

1K+ Views

Yes, deleting row from view delete row from base table. Let us understand this by creating a new table. The query to create a table is as followsmysql> create table deleteFromBaseTableDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(20) -> ); Query OK, 0 rows affected (0.83 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into deleteFromBaseTableDemo(Name) values('John'); Query OK, 1 row affected (0.18 sec) mysql> insert into deleteFromBaseTableDemo(Name) values('Carol'); Query OK, 1 row affected ... Read More

Get distinct values and count them in MySQL

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

420 Views

To get distinct values and count them, you can use GROUP BY clause.The syntax is as followsselect yourColumnName, count(*) as anyAliasName from yourTableName group by yourColumnName;To understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table GroupByAndCountDemo    -> (    -> ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> ClientName varchar(100)    -> ); Query OK, 0 rows affected (0.64 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into GroupByAndCountDemo(ClientName) values('John'); Query OK, 1 row affected (0.18 sec) mysql> insert ... Read More

Delete one row and reorder the others with the correct ID in MySQL?

George John
Updated on 30-Jul-2019 22:30:25

438 Views

To understand the concept, let us first create a table. The query to create a table is as followsmysql> create table ReorderSortDemo -> ( -> UserId int -> ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into ReorderSortDemo values(14); Query OK, 1 row affected (0.13 sec) mysql> insert into ReorderSortDemo values(4); Query OK, 1 row affected (0.10 sec) mysql> insert into ReorderSortDemo values(6); Query OK, 1 row affected (0.11 sec) mysql> insert into ReorderSortDemo values(3); Query ... Read More

MySQL query to combine two columns in a single column?

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

2K+ Views

You can use COALESCE() function for this. In the COALESCE() function, it returns the first NON NULL value from the column. To understand the concept, let us first create a demo tablemysql> create table combineTwoColumnsDemo    -> (    -> UserId int,    -> UserName varchar(20),    -> UserAge int    -> ); Query OK, 0 rows affected (1.12 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into combineTwoColumnsDemo values(101, 'John', 23); Query OK, 1 row affected (0.16 sec) mysql> insert into combineTwoColumnsDemo values(102, 'Carol', 20); Query OK, 1 row affected (0.14 ... Read More

How to determine if a value appears in a GROUP BY group in MySQL?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

667 Views

You can use aggregate function SUM() along with IF to determine if a value appears in a GROUP BY group.Let us first create a demo tablemysql> create table GroupbygroupDemo -> ( -> UserId int, -> UserName varchar(20) -> ); Query OK, 0 rows affected (1.48 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into GroupbygroupDemo values(10, 'John'); Query OK, 1 row affected (0.14 sec) mysql> insert into GroupbygroupDemo values(10, 'Carol'); Query OK, 1 row affected (0.08 sec) mysql> insert into ... Read More

Advertisements