MySQL query to find single value from duplicates with certain condition by excluding other records using NOT IN

AmitDiwan
Updated on 26-Sep-2019 07:08:35

44 Views

Let us first create a table −mysql> create table DemoTable (    Id int,    FirstName varchar(100) ); Query OK, 0 rows affected (0.69 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100, 'Chris'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(100, 'Robert'); Query OK, 1 row affected (0.48 sec) mysql> insert into DemoTable values(100, 'Mike'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values(100, 'Sam'); Query OK, 1 row affected (0.48 sec) mysql> insert into DemoTable values(101, 'David'); Query OK, 1 row affected (0.19 sec) mysql> insert ... Read More

Underscore as a table name in MySQL is possible?

AmitDiwan
Updated on 26-Sep-2019 07:05:44

170 Views

Yes, we can add underscore as a table name using backticks around the table name. Following is the syntax −INSERT INTO `yourTableName` values(yourValue1, .......N);Let us first create a table −mysql> create table `DemoTable_1` (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Location varchar(100) ); Query OK, 0 rows affected (1.17 sec)Insert some records in the table using insert command −mysql> insert into `DemoTable_1`(Location) values('C:/myFolder/JavaFiles'); Query OK, 1 row affected (0.20 sec) mysql> insert into `DemoTable_1`(Location) values('E:/AllJarOfJava'); Query OK, 1 row affected (0.17 sec) mysql> insert into `DemoTable_1`(Location) values('C:/ProgramFiles'); Query OK, 1 row affected (0.17 sec)Display all records from ... Read More

Delete records from a MySQL table by excluding non-deleted records using NOT IN

AmitDiwan
Updated on 26-Sep-2019 07:03:44

118 Views

Let us first create a table −mysql> create table DemoTable (    FirstName varchar(100) ); Query OK, 0 rows affected (0.65 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Mike'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Sam'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select ... Read More

Return only the first 15 characters from a column with string values in MySQL

AmitDiwan
Updated on 26-Sep-2019 07:01:47

433 Views

To return only the first 15 characters from string values, use the MySQL SUBSTR() function.Let us first create a table −mysql> create table DemoTable (    Title varchar(100) ); Query OK, 0 rows affected (0.69 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Introduction to MySQL'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('Introduction to Java'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('C in Depth with data structure and algorithm'); Query OK, 1 row affected (0.15 sec)Display all records from the table using select statement −mysql> ... Read More

Add a percentage (%) sign at the end to each value while using MySQL SELECT statement

AmitDiwan
Updated on 26-Sep-2019 06:59:14

3K+ Views

To add percentage sign at the end, use CONCAT() function. Let us first create a table −mysql> create table DemoTable (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentName varchar(100),    StudentScore int ); Query OK, 0 rows affected (0.68 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(StudentName, StudentScore) values('John', 65); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable(StudentName, StudentScore) values('Chris', 98); Query OK, 1 row affected (0.30 sec) mysql> insert into DemoTable(StudentName, StudentScore) values('Robert', 91); Query OK, 1 row affected (0.09 sec)Display all records from the table using select ... Read More

Add leading zeros to a MySQL column?

AmitDiwan
Updated on 26-Sep-2019 06:54:44

731 Views

To add leading zeros, you can use LPAD(). Let us first create a table −mysql> create table DemoTable (    Code varchar(100) ); Query OK, 0 rows affected (0.87 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('JS'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('CB'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('DM'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('CT'); Query OK, 1 row affected (0.07 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce ... Read More

How to convert yyyymmdd in INT type to date?

AmitDiwan
Updated on 26-Sep-2019 06:51:43

194 Views

For this, you can use the DATE() function. Let us first create a table −mysql> create table DemoTable (    Number int ); Query OK, 0 rows affected (0.48 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(20190108); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(20161231); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(20170411); Query OK, 1 row affected (0.09 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+----------+ | Number   | +----------+ | 20190108 | ... Read More

Find the average of column values in MySQL using aggregate function

AmitDiwan
Updated on 26-Sep-2019 06:48:43

54 Views

Let us first create a table −mysql> create table DemoTable (    Number int ); Query OK, 0 rows affected (0.79 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(56); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(78); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(89); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(98); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(91); Query OK, 1 row affected (0.35 sec) mysql> insert into DemoTable values(96); Query OK, 1 row affected ... Read More

Check if a directed graph is connected or not in C++

Arnab Chakraborty
Updated on 25-Sep-2019 14:54:52

1K+ Views

To check connectivity of a graph, we will try to traverse all nodes using any traversal algorithm. After completing the traversal, if there is any node, which is not visited, then the graph is not connected.For the directed graph, we will start traversing from all nodes to check connectivity. Sometimes one edge can have only outward edge but no inward edge, so that node will be unvisited from any other starting node.In this case the traversal algorithm is recursive DFS traversal.Input − Adjacency matrix of a graph0100000100000111000001000Output − The Graph is connected.Algorithmtraverse(u, visited) Input: The start node u and the ... Read More

Fleury’s Algorithm for printing Eulerian Path or Circuit in C++

Arnab Chakraborty
Updated on 25-Sep-2019 14:53:48

1K+ Views

Fleury’s Algorithm is used to display the Euler path or Euler circuit from a given graph. In this algorithm, starting from one edge, it tries to move other adjacent vertices by removing the previous vertices. Using this trick, the graph becomes simpler in each step to find the Euler path or circuit.We have to check some rules to get the path or circuit −The graph must be a Euler Graph.When there are two edges, one is bridge, another one is non-bridge, we have to choose non-bridge at first.sChoosing of starting vertex is also tricky, we cannot use any vertex as ... Read More

Advertisements