Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare row count of two tables in a single query and return boolean

I want to compare row count of two tables and then return 0 or 1 depending on whether its same or not.

I am thinking of something like this but can't move ahead and need some help.

SELECT 
       CASE WHEN (select count(*) from table1)=(select count(*) from table2)
       THEN 1
       ELSE 0
       END AS RowCountResult
FROM Table1,Table2

I am getting multiple rows instead of a single row with 0 or 1

like image 639
Naveen Avatar asked Dec 11 '14 08:12

Naveen


People also ask

How do I compare the number of rows in SQL?

SELECT (SELECT count(*) from table1)=(SELECT count(*) from table2) AS RowCountResult; as a boolean result will be returned.

How do you compare records in two tables?

Use the Find Unmatched Query Wizard to compare two tables One the Create tab, in the Queries group, click Query Wizard. In the New Query dialog box, double-click Find Unmatched Query Wizard. On the first page of the wizard, select the table that has unmatched records, and then click Next.

How can I get common values from two tables in SQL?

The SQL intersect operator allows us to get common values between two tables or views. The following graphic shows what the intersect does. The set theory clearly explains what an intersect does. In mathematics, the intersection of A and B (A ∩ B) is the set that contains all elements of A that also belong to B.

How can I compare two rows in the same table in SQL?

Here's the SQL query to compare each row with previous row. In the above query, we join sales table with itself using an INNER JOIN condition g2.id=g1.id + 1 that allows you to compare each row with its previous row. Please note, this condition depends on the fact that our id column has consecutive numbers.


1 Answers

you have to remove :

FROM Table1,Table2

Otherwise it will consider the result of the Case-When for each row of this FROM clause.

like image 93
Banov Avatar answered Sep 20 '22 04:09

Banov