Get Row Count of Two Tables in Different Databases in a Single Query

karthikeya Boyini
Updated on 30-Jun-2020 12:21:37

455 Views

For this, you can use aggregate function COUNT(*). Let us first create a table in let’s say database “web” −mysql> create table DemoTable1    -> (    -> Value int    -> ); Query OK, 0 rows affected (0.60 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values(10); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable1 values(20); Query OK, 1 row affected (0.15 sec)Display all records from the table using select statement −mysql> select *from DemoTable1;OutputThis will produce the following output −+-------+ | Value | +-------+ |    10 | | ... Read More

Order Alphanumeric Column in MySQL

Sharon Christine
Updated on 30-Jun-2020 12:16:32

1K+ Views

To order an alphanumeric column with values like “100X, “2Z”, etc. use the ORDER BY. Let us first create a table −mysql> create table DemoTable -> ( -> StudentId varchar(100) -> ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2X'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('100Y'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('100X'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('2Z'); Query OK, 1 row affected (0.14 sec) mysql> ... Read More

System Variables vs Local Variables in MySQL

Chandu yadav
Updated on 30-Jun-2020 12:16:18

465 Views

The local variable has the scope for only a set of statements or block of statement. Whenever a set of statements or block of statement has completed then local variable goes out of scope.For ExampleLocal variable can be used in stored procedure, function etc. It is used with the DECLARE keyword.The syntax is as follows to local variables.DECLARE yourVariableName dataType;The global variables or system variables has the scope across connections until server restart. It is set using GLOBAL keyword. The syntax is as follows −SET GLOBAL max_connections=value; OR SET @@global.max_connections=value;If you know port number then you can use system variable ... Read More

Use IF NOT IN in a MySQL Procedure

Kumar Varma
Updated on 30-Jun-2020 12:15:04

165 Views

Let us first see the syntax of IF NOT IN in MySQL −if(yourVariableName  NOT IN (yourValue1, yourValue2, ........N) ) then    statement1 else    statement2 endif    Let us implement the above syntax to use IF NOT IN −mysql> DELIMITER // mysql> CREATE PROCEDURE IF_NOT_INDemo(IN value int)    ->    BEGIN    ->       if(value NOT IN  (10, 20, 30) ) then    ->          select "Value Not Found";    ->       else    ->          select "Value Found";    ->       end if;    ->    END ... Read More

Place Number 0 at the End in MySQL Maintaining Ascending Order

Sharon Christine
Updated on 30-Jun-2020 12:13:44

103 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Number int    -> ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(9); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(4); Query OK, 1 row affected (0.31 sec) mysql> insert into DemoTable values(0); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(7); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(11); Query OK, 1 row affected (0.12 sec)Display all ... Read More

Fix Error with Type Heap for Temporary Tables in MySQL

Sharon Christine
Updated on 30-Jun-2020 12:11:41

269 Views

The TYPE=HEAP deprecated in newer MySQL versions. You can use ENGINE=HEAP instead of TYPE. Following is the syntax −ENGINE=HEAP;Let us first create a table. Here, we have set Engine=HEAP −mysql> create TEMPORARY table DemoTable    -> (    -> StudentId int,    -> StudentName varchar(30)    -> )Engine = HEAP; Query OK, 0 rows affected (0.00 sec)Let us check the definition of table −mysql> show create table DemoTable;OutputThis will produce the following output −+--------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table        | Create Table | +--------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | DemoTable | CREATE TEMPORARY TABLE `DemoTable` (`StudentId` int(11) DEFAULT NULL, `StudentName` varchar(30) COLLATE utf8_unicode_ci DEFAULT ... Read More

Insert Auto Increment in an Already Created Table in MySQL

karthikeya Boyini
Updated on 30-Jun-2020 12:03:12

242 Views

Use ALTER command for this. Let us first create a table −mysql> create table DemoTable -> ( -> StudentName varchar(100) -> ); Query OK, 0 rows affected (0.46 sec)Here is the query to insert auto_increment −mysql> alter table DemoTable ADD COLUMN StudentId int NOT NULL; Query OK, 0 rows affected (0.50 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> alter table DemoTable ADD PRIMARY KEY(StudentId); Query OK, 0 rows affected (1.23 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> alter table DemoTable CHANGE StudentId StudentId int NOT NULL AUTO_INCREMENT; Query OK, 0 rows affected (2.20 sec) Records: 0 ... Read More

Finding Number of Occurrences of a Specific String in MySQL

karthikeya Boyini
Updated on 30-Jun-2020 12:02:21

613 Views

Use LENGTH() for this. Let us first create a table −mysql> create table DemoTable -> ( -> Value text -> ); Query OK, 0 rows affected (0.74 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('10, 20, 10, 30, 10, 40, 50, 40'); Query OK, 1 row affected (0.24 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+-------------------------+ | Value | +-------------------------+ | 10, 20, 10, 30, ... Read More

How to See What a MySQL View Is Made Of

karthikeya Boyini
Updated on 30-Jun-2020 11:54:49

178 Views

Following is the syntax −show create view yourViewName;Let us first create a table −mysql> create table DemoTable -> ( -> StudentName varchar(20) -> ); Query OK, 0 rows affected (0.56 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.13 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+-------------+ | StudentName | +-------------+ | ... Read More

Order By Two Fields and Null Values in MySQL

Sharon Christine
Updated on 30-Jun-2020 11:51:42

257 Views

Let us first create a table −mysql> create table DemoTable -> ( -> FirstName varchar(100), -> LastName varchar(100) -> ); Query OK, 0 rows affected (1.39 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Sam', 'Brown'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable values(null, 'Smith'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('David', 'Taylor'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values('Mike', null); Query OK, 1 row affected (0.45 sec)Display all records from the table using select statement −mysql> select ... Read More

Advertisements