Found 6702 Articles for Database

Effective way to add integers based on table values in MySQL?

AmitDiwan
Updated on 12-Nov-2019 07:00:07

47 Views

You need to use GROUP BY clause. Let us first create a −mysql> create table DemoTable1443    -> (    -> StudentId int,    -> StudentScore int    -> ); Query OK, 0 rows affected (0.42 sec)Insert some records in the table using insert −mysql> insert into DemoTable1443 values(100, 78); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable1443 values(101, 89); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1443 values(100, 88); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1443 values(101, 97); Query OK, 1 row affected (0.12 sec)Display all records from the ... Read More

How to sum rows of VARCHAR datatype or TIME datatype in MySQL?

AmitDiwan
Updated on 12-Nov-2019 06:58:28

202 Views

Let us first create a −mysql> create table DemoTable1442    -> (    -> DueTime time    -> ); Query OK, 0 rows affected (0.56 sec)Insert some records in the table using insert −mysql> insert into DemoTable1442 values('00:08:00'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1442 values('00:04:00'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1442 values('12:55:00'); Query OK, 1 row affected (0.15 sec)Display all records from the table using select −mysql> select * from DemoTable1442;This will produce the following output −+----------+ | DueTime  | +----------+ | 00:08:00 | | 00:04:00 | | 12:55:00 | ... Read More

What is the purpose of ORDER BY columnname*1 in MySQL?

AmitDiwan
Updated on 12-Nov-2019 06:57:12

52 Views

MySQL will implicitly convert the column into a number. Following is the syntax −select * from yourTableName  order by yourColumnName*1;Let us first create a −mysql> create table DemoTable1441    -> (    -> Id varchar(30)    -> ); Query OK, 0 rows affected (0.53 sec)Insert some records in the table using insert −mysql> insert into DemoTable1441 values('301'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable1441 values('23'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable1441 values('345'); Query OK, 1 row affected (0.42 sec) mysql> insert into DemoTable1441 values('10'); Query OK, 1 row affected (0.23 sec) ... Read More

How to set default value to NULL in MySQL?

AmitDiwan
Updated on 12-Nov-2019 06:56:10

4K+ Views

Use DEFAULT keyword in MySQL to set default value to NULL. Let us first create a −mysql> create table DemoTable1440    -> (    -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentName varchar(20) DEFAULT NULL,    -> StudentAge int DEFAULT NULL    -> ); Query OK, 0 rows affected (0.55 sec)Insert some records in the table using insert command. For values left blank, the default gets inserted −mysql> insert into DemoTable1440(StudentName, StudentAge) values('Chris', 21); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1440 values(); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1440(StudentName) ... Read More

How to check if a specific country code exists in a cell with MySQL?

AmitDiwan
Updated on 12-Nov-2019 06:54:37

190 Views

For specific value, use FIND_IN_SET(). Let us first create a −mysql> create table DemoTable1439    -> (    -> CountryId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> CountryCode varchar(20)    -> ); Query OK, 0 rows affected (0.49 sec)Insert some records in the table using insert −mysql> insert into DemoTable1439(CountryCode) values('1022_US, 7894_UK'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1439(CountryCode) values('6567_AUS, 7894_UK'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1439(CountryCode) values('6567_AUS'); Query OK, 1 row affected (0.09 sec)Display all records from the table using select −mysql> select * from DemoTable1439;This will produce the ... Read More

How to save JSON array to MySQL database?

AmitDiwan
Updated on 12-Nov-2019 06:52:58

2K+ Views

For this, you can use JSON data type from MySQL. Let us first create a −mysql> create table DemoTable1438    -> (    -> EmployeeDetails json    -> ); Query OK, 0 rows affected (5.97 sec)Insert some records in the table using insert −mysql> insert into DemoTable1438 values('[{"EmployeeId":"EMP-101","EmployeeName":"Chris"},{"EmployeeId":"EMP-102","EmployeeName":"David"},{"EmployeeId":"EMP-103","EmployeeName":"Sam"}]'); Query OK, 1 row affected (0.22 sec)Display all records from the table using select −mysql> select * from DemoTable1438;This will produce the following output −+------------------------------------------------------------------------------------------------------------------------------------------------------------+ | EmployeeDetails                                                                                                                                            | +------------------------------------------------------------------------------------------------------------------------------------------------------------+ | [{"EmployeeId": "EMP-101", "EmployeeName": "Chris"}, {"EmployeeId": "EMP-102", "EmployeeName": "David"}, {"EmployeeId": "EMP-103", "EmployeeName": "Sam"}] | +------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)

Create index of three columns in MySQL?

AmitDiwan
Updated on 12-Nov-2019 06:50:10

136 Views

For index, you can use KEY(). Let us first create a −mysql> create table DemoTable1437    -> (    -> StudentId int,    -> StudentName varchar(20),    -> StudentMarks int,    -> StudentAge int    -> ,    -> KEY(StudentId, StudentMarks, StudentAge)    -> ); Query OK, 0 rows affected (0.97 sec)Following is the query to check the description of −mysql> desc DemoTable1437;This will produce the following output −+--------------+-------------+------+-----+---------+-------+ | Field        | Type        | Null | Key | Default | Extra | +--------------+-------------+------+-----+---------+-------+ | StudentId    | int(11)     | YES  | MUL ... Read More

MySQL query to update different fields based on a condition?

AmitDiwan
Updated on 12-Nov-2019 06:46:44

196 Views

Let us first create a −mysql> create table DemoTable1436    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(20)    -> ); Query OK, 0 rows affected (1.06 sec)Insert some records in the table using insert −mysql> insert into DemoTable1436(Name) values('Chris'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable1436(Name) values('David'); Query OK, 1 row affected (0.40 sec) mysql> insert into DemoTable1436(Name) values('Bob'); Query OK, 1 row affected (0.35 sec) mysql> insert into DemoTable1436(Name) values('David'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable1436(Name) values('David'); Query OK, 1 row affected ... Read More

MySQL procedure to call multiple procedures?

AmitDiwan
Updated on 12-Nov-2019 06:44:34

913 Views

Let us first see the syntax, wherein we are calling multiple procedures from a stored procedure −DELIMITER // CREATE PROCEDURE yourProcedureName() BEGIN    CALL yourStoredProcedureName1();    CALL yourStoredProcedureName2();    .    .    N END // DELIMITER //Let us implement the above syntax to call multiple stored procedures.Following is the query to create first stored procedure −mysql> DELIMITER // mysql> CREATE PROCEDURE hello_message()    -> BEGIN    -> SELECT 'HELLO WORLD !!';    -> END    -> // Query OK, 0 rows affected (0.19 sec)The query to create second stored procedure is as follows −mysql> CREATE PROCEDURE hi_message()    -> ... Read More

Order dates in MySQL with the format “01 August 2019”?

AmitDiwan
Updated on 12-Nov-2019 06:42:24

63 Views

To display dates like “01 August 2019”, use ORDER BY STR_TO_DATE(). Let us first create a −mysql> create table DemoTable1435    -> (    -> DueDate varchar(60)    -> ); Query OK, 0 rows affected (1.08 sec)Insert some records in the table using insert −mysql> insert into DemoTable1435 values('01 August 2019'); Query OK, 1 row affected (0.34 sec) mysql> insert into DemoTable1435 values('01 Feb 2018'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1435 values('31 Jan 2017'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable1435 values('01 March 2019'); Query OK, 1 row affected (0.11 sec)Display ... Read More

Advertisements