Found 6702 Articles for Database

MySQL query to convert empty values to NULL?

AmitDiwan
Updated on 04-Nov-2019 10:55:35

861 Views

It’s easy to convert empty values to NULL using SET and WHERE. Let us first create a table −mysql> create table DemoTable1315 -> ( -> CountryName varchar(10) -> ); Query OK, 0 rows affected (0.60 sec)Insert some records in the table using insert command. We have set some empty values here as well −mysql> insert into DemoTable1315 values('US'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1315 values(''); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable1315 values('UK'); Query OK, 1 row affected (0.70 sec) mysql> insert into DemoTable1315 values(''); Query OK, 1 row affected (0.12 ... Read More

How to use a comma-separated string in an `IN ()` in MySQL?

AmitDiwan
Updated on 04-Nov-2019 10:46:30

2K+ Views

Set the comma-separated string in the IN() as in the below syntax:select *from yourTableName where yourColumnName IN('yourCommaSeparatedValue');Let us first create a table −mysql> create table DemoTable1314 -> ( -> Number varchar(100) -> ); Query OK, 0 rows affected (0.53 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1314 values('45, 67, 89'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1314 values('10, 20, 50'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1314 values('90, 56, 45'); Query OK, 1 row affected (0.23 sec)Display all records from the table using select statement −mysql> select ... Read More

MySQL query to display records ordered by numeric difference?

AmitDiwan
Updated on 04-Nov-2019 10:46:31

52 Views

Use ORDER BY and set the difference to display records ordered by numeric difference. Following is the syntax −select *from yourTableName order by (yourIntegerColumnName1 - yourIntegerColumnName2);Let us first create a table −mysql> create table DemoTable1313 -> ( -> Name varchar(20), -> Score1 int, -> Score2 int -> ); Query OK, 0 rows affected (3.48 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1313 values('Chris', 40, 60); Query OK, 1 row affected (0.38 sec) mysql> insert into DemoTable1313 values('David', 70, 50); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1313 values('Adam', 35, 30); Query OK, ... Read More

A single query to get the sum of count from different tables in MySQL?

AmitDiwan
Updated on 04-Nov-2019 10:42:49

2K+ Views

To get the sum of count from different tables, use UNION ALL. Let us first create a table −mysql> create table DemoTable1 -> ( -> Id int, -> Name varchar(30) -> ); Query OK, 0 rows affected (1.55 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values(10, 'Chris Brown'); Query OK, 1 row affected (0.83 sec) mysql> insert into DemoTable1 values(20, 'David Miller'); Query OK, 1 row affected (0.50 sec) mysql> insert into DemoTable1 values(30, 'John Adam'); Query OK, 1 row affected (0.83 sec)Display all records from the table using select statement −mysql> select *from ... Read More

Minimization of ER Diagram

Narendra Kumar
Updated on 22-Oct-2019 12:13:03

2K+ Views

Problem statementER diagram is pictorial representation of shows various tables and relations amongst them. ER diagram we can reduce the number of database.One to one cardinalityLet us consider below diagram with one to one cardinality −Above ER diagram represents 3 entities −Employee entity has 2 attributes namely emp_name. emp_id is the primary keyCompany entity has 2 attributes namely cmp_name. cmp_id is the primary keyPrimary key of Work entity can be emp_id or cmp_idWe cannot combine 3 tables into single one can either merge Work into Employee or Company. minimum 2 tables are required in one to one cardinality scenario.One to ... Read More

Using variables with MySQL prepare statement

AmitDiwan
Updated on 10-Oct-2019 12:34:09

115 Views

Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    FirstName varchar(20),    LastName varchar(20) ); Query OK, 0 rows affected (0.53 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(FirstName, LastName) values('John', 'Smith'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(FirstName, LastName) values('David', 'Miller'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(FirstName, LastName) values('John', 'Doe'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(FirstName, LastName) values('Chris', 'Brown'); Query OK, 1 row affected (0.15 sec)Display all records from ... Read More

MySQL query to count occurrences of distinct values and display the result in a new column?

AmitDiwan
Updated on 10-Oct-2019 12:31:39

479 Views

Let us first create a table −mysql> create table DemoTable (    Value int ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(80); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(90); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(90); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(80); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(90); Query OK, 1 row affected ... Read More

Get the sum of last 3 digits from all the values in a column with MySQL

AmitDiwan
Updated on 10-Oct-2019 12:29:08

156 Views

Since we want the sum of last 3 digits, we need to use aggregate function SUM() along with RIGHT(). Let us first create a table −mysql> create table DemoTable (    Code int ); Query OK, 0 rows affected (0.77 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(5464322); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values(90884); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(23455644); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(4353633); Query OK, 1 row affected (0.11 sec)Display all records from ... Read More

How to update a value with substring of current value by removing the separator and numbers after a separator in MySQL?

AmitDiwan
Updated on 10-Oct-2019 12:27:38

419 Views

Here, let’s say you have a string with form “StringSeparatorNumber” form like John/56989. Now if you want to remove the number after separator /, then use the SUBSTRING_INDEX(). Let us first create a table −mysql> create table DemoTable (    StudentName varchar(100) ); Query OK, 0 rows affected (1.05 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John/56989'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Carol'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('David/74674'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values('Bob/45565'); Query ... Read More

How can I find and replace in MySQL in a column with file path?

AmitDiwan
Updated on 10-Oct-2019 12:25:27

355 Views

For thus, use MySQL REPLACE(). Let us first create a table −mysql> create table DemoTable (    FolderLocation text ); Query OK, 0 rows affected (0.80 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('C/ProgramFiles/AllMySQLProgram'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('C/ProgramFiles/JavaChatApplication'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('C/ProgramFiles/Main/Image.png'); Query OK, 1 row affected (0.18 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+------------------------------------+ | FolderLocation                   ... Read More

Advertisements