Found 4378 Articles for MySQL

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

MySQL query to get the count of rows in which two or more specified values appear?

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

58 Views

To get the count of rows in which two or more specified values appear, let us first create a sample table:mysql> create table specifiedValuesDemo -> ( -> Value int, -> Value2 int, -> Value3 int -> ); Query OK, 0 rows affected (0.60 sec)Following is the query to insert some records in the table using insert command:mysql> insert into specifiedValuesDemo values(10, 15, 20); Query OK, 1 row affected (0.17 sec) mysql> insert into specifiedValuesDemo values(40, 10, 20); Query OK, 1 row affected (0.16 sec) ... Read More

Write a MySQL query equivalent to “SHOW TABLES” in sorted order?

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

81 Views

Use INFORMATION_SCHEMA.TABLES to display tables in sorted order. The below syntax will give sorted list of tables in ascending order:select TABLE_NAME from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA= 'yourDatabaseName' order by TABLE_NAME;Following is the query to implement the equivalent to SHOW TABLES:mysql> select TABLE_NAME from INFORMATION_SCHEMA.TABLES    -> where TABLE_SCHEMA= 'sample' order by TABLE_NAME;This will produce the following output+------------------------------------+ | TABLE_NAME                         | +------------------------------------+ | a                                  | | accumulateddemo               ... Read More

How to suppress MySQL stored procedure output?

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

846 Views

To suppress MySQL stored procedure output, you can use variable. Let us first create a table.mysql> create table person_information    -> (    -> Id int,    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.50 sec)Following is the query to insert some records in the table using insert command:mysql> insert into person_information values(100, 'John'); Query OK, 1 row affected (0.17 sec) mysql> insert into person_information values(101, 'Chris'); Query OK, 1 row affected (0.22 sec) mysql> insert into person_information values(102, 'Robert'); Query OK, 1 row affected (0.16 sec)Following is the query to display records from ... Read More

How to add column using alter in MySQL?

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

141 Views

Following is the syntax to add column using alter in MySQL:alter table yourTableName add column yourColumnName yourDataType default yourValue;Let us first create a table:mysql> create table alterTableDemo    -> (    -> Id int,    -> Name varchar(10)    -> ); Query OK, 0 rows affected (0.69 sec)Let us check the description of the table using DESC command. This displays Field, Type, Key, etc. of the table:mysql> desc alterTableDemo;This will produce the following output+-------+-------------+------+-----+---------+-------+ | Field | Type        | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | Id    | int(11)     | YES ... Read More

Can we order a MySQL result with mathematical operations?

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

201 Views

Yes, we can order with mathematical operations using ORDER BY clause. Let us first create a table:mysql> create table orderByMathCalculation    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Quantity int,    -> Price int    -> ); Query OK, 0 rows affected (0.57 sec)Following is the query to insert some records in the table using insert command:mysql> insert into orderByMathCalculation(Quantity, Price) values(10, 50); Query OK, 1 row affected (0.21 sec) mysql> insert into orderByMathCalculation(Quantity, Price) values(20, 40); Query OK, 1 row affected (0.14 sec) mysql> insert into orderByMathCalculation(Quantity, Price) values(2, 20); Query ... Read More

Advertisements