MySQLi Articles

Page 260 of 341

MySQL If statement with multiple conditions?

Samual Sam
Samual Sam
Updated on 30-Jul-2019 8K+ Views

You can use if statement in a stored procedure with multiple conditions with the help of AND or OR operator. The syntax is as follows −DECLARE X int; DECLARE Y int; SET X = value1; SET Y = value2; IF ( (X < Y AND X > value1 AND Y >value2) OR X! = anyValueToCompare) THEN    yourStatement; ELSE    yourStatement; END IFNow to understand the above syntax, let us create a stored procedure. The query to create a stored procedure is as follows −mysql> create procedure SP_IFELSEDEMO()    -> BEGIN    -> DECLARE X int;    -> DECLARE Y ...

Read More

Sum if all rows are not null else return null in MySQL?

karthikeya Boyini
karthikeya Boyini
Updated on 30-Jul-2019 638 Views

You can achieve this with the help of GROUP BY HAVING clause. The syntax is as follows −SELECT yourColumnName1,    SUM(yourCoumnName2)    from yourTableName    GROUP BY yourColumnName1 HAVING COUNT(yourCoumnName2) = COUNT(*);To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table SumDemo    -> (    -> Id int,    -> Amount int    -> ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into SumDemo values(1, 200); Query OK, 1 row affected (0.22 ...

Read More

MySQL &ldquo;order by&rdquo; inside of &ldquo;group by&rdquo;? Is it possible?

karthikeya Boyini
karthikeya Boyini
Updated on 30-Jul-2019 880 Views

Instead of using ORDER BY inside GROUP BY, you can use the MAX() aggregate function.The syntax is as follows −SELECT yourNameColumnName, MAX(yourRankColumnName) FROM yourTableName GROUP BY yourNameColumnName;To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table MaxDemo    -> (    -> UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> UserName varchar(20),    -> UserRank int    -> ); Query OK, 0 rows affected (0.77 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into MaxDemo(UserName, UserRank) values('Larry', 2); Query ...

Read More

Display the warning message when a FLOAT value is inserted into DECIMAL in MySQL?

karthikeya Boyini
karthikeya Boyini
Updated on 30-Jul-2019 200 Views

You can create a temporary table with data type DECIMAL to get a warning when a float value is inserted into an int column. Display the same warning using SHOW WARNINGS.Let us create a table to understand. The query is as follows to create a table.mysql> create temporary table WarningDemo    -> (    -> Value DECIMAL    -> ); Query OK, 0 rows affected (0.13 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into WarningDemo values(9.80); Query OK, 1 row affected, 1 warning (0.03 sec)Here we are getting a warning. Let ...

Read More

Create an aggregate checksum of a column in MySQL

karthikeya Boyini
karthikeya Boyini
Updated on 30-Jul-2019 653 Views

You can use CRC32 checksum for this. The syntax is as follows −SELECT SUM(CRC32(yourColumnName)) AS anyAliasName FROM yourTableName;To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table CRC32Demo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> UserId varchar(20)    -> ); Query OK, 0 rows affected (0.67 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into CRC32Demo(UserId) values('USER-1'); Query OK, 1 row affected (0.38 sec) mysql> insert into CRC32Demo(UserId) values('USER-123'); Query OK, 1 row ...

Read More

Does MySQL support table inheritance?

karthikeya Boyini
karthikeya Boyini
Updated on 30-Jul-2019 2K+ Views

MySQL uses foreign key constraint instead of inheritance. MySQL does not support table inheritance.You can achieve the same with the help of foreign key constraint. Let us create a table and use the foreign key constraint. The query to create the first table is as follows −mysql> create table Parent_Table -> ( -> ParentId int, -> PRIMARY KEY(ParentId) -> ); Query OK, 0 rows affected (3.59 sec)Now create the second table. The query to create the second table is as follows −mysql> create table Child_Table ...

Read More

How do I get the average string length in MySQL?

karthikeya Boyini
karthikeya Boyini
Updated on 30-Jul-2019 1K+ Views

To get the average string length in MySQL, we will work around a query that gets rows from 1 to 10 and displays the result.Let us first create a table. The query to create a table is as follows −mysql> create table AverageString -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Value varchar(20) -> ); Query OK, 0 rows affected (0.66 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into AverageString(Value) values('MySQL Query'); Query OK, 1 row ...

Read More

Set Optimal MySQL configuration in my.cnf?

karthikeya Boyini
karthikeya Boyini
Updated on 30-Jul-2019 276 Views

First, you need to open my.cnf file. The following is the query to get the directory location of the config file on Windows −mysql> select @@datadir;Output+---------------------------------------------+ | @@datadir | +---------------------------------------------+ | C:\ProgramData\MySQL\MySQL Server 8.0\Data\ | +---------------------------------------------+ 1 row in set (0.00 sec)Here is the snapshot of the directory −Now open my.cnf file. The snapshot is as follows −If you want to add more data to cache, ...

Read More

How to achieve case sensitive uniqueness and case insensitive search in MySQL?

Samual Sam
Samual Sam
Updated on 30-Jul-2019 1K+ Views

You can achieve case sensitive uniqueness and case insensitive search with the help of the following two ways −VARBINARY data type_bin collationVARBINARY data typeTo work with the VARBINARY data type, let us first create a table. The query to create a table is as follows −mysql> create table SearchingDemo2 -> ( -> UserId VARBINARY(128) NOT NULL, -> UNIQUE KEY index_on_UserId2(UserId ) -> )ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; Query OK, 0 rows affected, 1 warning (0.99 sec)Keep in mind UserId has data type VARBINARY(128) and Index(‘index_on_UserId2’) on a column ‘UserId’._bin ...

Read More

How to know the exact number of table and columns in a MySQL database?

Samual Sam
Samual Sam
Updated on 30-Jul-2019 301 Views

To get the exact number if table and columns in a MySQL database, use the DISTINCT inside COUNT().Let’s say we have a database ‘sample’ and we need to work on it to get the exact number of table and columns.To achieve it, the query is as follows −mysql> SELECT COUNT(DISTINCT TABLE_NAME) AS TotalTable, Count(Column_Name) AS TOTALColumn -> FROM INFORMATION_SCHEMA.COLUMNS -> WHERE TABLE_SCHEMA = 'sample';The following is the output displaying the count of table and columns in the database ‘sample’ −+------------+-------------+ | TotalTable | TOTALColumn | +------------+-------------+ | ...

Read More
Showing 2591–2600 of 3,404 articles
« Prev 1 258 259 260 261 262 341 Next »
Advertisements