Found 4219 Articles for MySQLi

How to use “OR” condition in MySQL CASE expression?

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

156 Views

Set the same condition like “OR” in a MySQL CASE expression. Let us first create a sample table.Following is the querymysql> create table caseOrConditionDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(100),    -> Score int    -> ); Query OK, 0 rows affected (0.49 sec)Following is the query to insert some records in the table using insert command:mysql> insert into caseOrConditionDemo(Name, Score) values('Larry', 85); Query OK, 1 row affected (0.18 sec) mysql> insert into caseOrConditionDemo(Name, Score) values('Sam', 74); Query OK, 1 row affected (0.20 sec) mysql> insert into caseOrConditionDemo(Name, ... Read More

How to declare a variable correctly in a MySQLProcedure?

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

149 Views

The variable declaration must be between BEGIN and END. Under BEGIN and END, the first statement must be declaration of variable. After that you can include insert, select, etc.Let us now see an example. Here, the variable name is “output”:mysql> DELIMITER // mysql> CREATE PROCEDURE showVariablesValue() -> BEGIN -> DECLARE output varchar(100); -> SET output="Hello MySQL"; -> SELECT output; -> END -> // Query OK, 0 rows affected (0.25 sec) mysql> DELIMITER ;Now you can call the stored procedure using call ... Read More

Does MySQL converts bool to tinyint(1) internally?

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

1K+ Views

Yes, MySQL internally convert bool to tinyint(1) because tinyint is the smallest integer data type.You can also say the bool is synonym for tinyint(1). Let us first create a sample table:mysql> create table boolToTinyIntDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(20), -> isAgeGreaterThan18 bool -> ); Query OK, 0 rows affected (1.02 sec)Let us now check the description of table:mysql> desc boolToTinyIntDemo;This will produce the following output+--------------------+-------------+------+-----+---------+----------------+ | Field ... Read More

MySQL query to select one specific row and another random row?

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

142 Views

To select one specific row and another random row, you can use ORDER BY and RAND(). Let us first create a sample table:mysql> create table oneSpecificRowAndOtherRandom    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.72 sec)Following is the query to insert some records in the table using insert command:mysql> insert into oneSpecificRowAndOtherRandom(Name) values('Larry'); Query OK, 1 row affected (0.56 sec) mysql> insert into oneSpecificRowAndOtherRandom(Name) values('Sam'); Query OK, 1 row affected (0.13 sec) mysql> insert into oneSpecificRowAndOtherRandom(Name) values('Mike'); Query OK, 1 row affected ... Read More

In a MySQL schema, what is the meaning of “AUTO_INCREMENT=3”

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

227 Views

In MySQL, AUTO_INCREMENT=3 tells that the inserted record will start from 3 not the default 1. Let us first create a sample table and set auto increment to 3:mysql> create table Auto_incrementDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(20)    -> )AUTO_INCREMENT=3; Query OK, 0 rows affected (0.52 sec)Following is the query to insert some records in the table using insert command:mysql> INSERT INTO Auto_incrementDemo(Name) values('John'); Query OK, 1 row affected (0.12 sec) mysql> INSERT INTO Auto_incrementDemo(Name) values('Larry'); Query OK, 1 row affected (0.15 sec) mysql> INSERT INTO Auto_incrementDemo(Name) ... Read More

Count value for multiple columns in MySQL?

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

508 Views

To count value for multiple columns, use the CASE statement. Let us first create a table::mysql> create table countValueMultipleColumnsDemo    -> (    -> Value1 int,    -> Value2 int,    -> Value3 int    -> ); Query OK, 0 rows affected (0.62 sec)Following is the query to insert some records in the table using insert command:mysql> insert into countValueMultipleColumnsDemo values(10, 15, 10); Query OK, 1 row affected (0.15 sec) mysql> insert into countValueMultipleColumnsDemo values(20, 30, 10); Query OK, 1 row affected (0.14 sec) mysql> insert into countValueMultipleColumnsDemo values(40, 10, 60); Query OK, 1 row affected (0.18 sec)Following ... Read More

How to change a primary key in MySQL to auto_increment?

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

4K+ Views

To change a primary key to auto_increment, you can use MODIFY command. Let us first create a table.mysql> create table changePrimaryKeyInAutoIncrement    -> (    -> StudentId int not null primary key,    -> StudentName varchar(100),    -> StudentAge int,    -> StudentAddress varchar(100)    -> ); Query OK, 0 rows affected (0.63 sec)Let us now check the description of table using desc command:mysql> desc changePrimaryKeyInAutoIncrement;This will produce the following output+----------------+--------------+------+-----+---------+-------+ | Field          | Type         | Null | Key | Default | Extra | +----------------+--------------+------+-----+---------+-------+ | StudentId      | int(11)   ... Read More

Total number of fields in all tables in database?

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

545 Views

To get total number of fields in all tables in database, you can use information_schema.columns along with aggregate function count(*).We are using ‘sample’ database which consists of a lot of tables with fields. Following is the query to get total number of fields in all tables in database:mysql> SELECT COUNT(*) AS TOTAL_NUMBER_OF_FIELDS    -> FROM INFORMATION_SCHEMA.COLUMNS    -> WHERE TABLE_SCHEMA = 'sample';This will produce the following output+------------------------+ | TOTAL_NUMBER_OF_FIELDS | +------------------------+ | 796                    | +------------------------+ 1 row in set (0.04 sec)Now, let us check another database ‘test’. Following is the query ... Read More

How to convert bool to int in MySQL?

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

1K+ Views

To convert bool to int in MySQL, you can use CAST(). Let us first create a table:mysql> create table convertBoolToIntDemo -> ( -> isYoung bool -> ); Query OK, 0 rows affected (0.69 sec)Following is the query to insert some records in the table using insert command:mysql> insert into convertBoolToIntDemo values(true); Query OK, 1 row affected (0.18 sec) mysql> insert into convertBoolToIntDemo values(false); Query OK, 1 row affected (0.09 sec) mysql> insert into convertBoolToIntDemo values(true); Query OK, 1 row affected (0.15 sec) mysql> insert into convertBoolToIntDemo values(false); Query ... Read More

Sort by character length in MySQL

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

3K+ Views

To sort by character length in MySQL use the ORDER BY LENGTH(). Let us first create a table:mysql> create table orderingAADemo    -> (    -> Value varchar(100)    -> ); Query OK, 0 rows affected (1.30 sec)Following is the query to insert some records in the table using insert command:mysql> insert into orderingAADemo values('A'); Query OK, 1 row affected (0.12 sec) mysql> insert into orderingAADemo values('B'); Query OK, 1 row affected (0.13 sec) mysql> insert into orderingAADemo values('AA'); Query OK, 1 row affected (0.20 sec) mysql> insert into orderingAADemo values('C'); Query OK, 1 row affected (0.12 ... Read More

Advertisements