Reputation: 336
I have three tables.
deliverable_category deliverable passed_deliverable
i want to compare these two tables(deliverable & passed_deliverable),check if thier number of rows of thesame deliverable category is equal.
here is what i have :
$query2=mysql_query("SELECT * FROM deliverable WHERE deliverable_category_id=3 ");
if($row2=mysql_num_rows($query2)>0){
$query3=mysql_query("SELECT * FROM passed_deliverable WHERE deliverable_category_id=3");
}
if($row3=mysql_num_rows($query3)>0){
if ($row2=$row3){
echo "EQUAL ";
}
}
else {
echo "NOT EQUAL ";
}
Upvotes: 2
Views: 1401
Reputation: 1108
?? "check if thier number of rows of thesame deliverable category is equal."
Can you do
SELECT IF((SELECT COUNT( * )
FROM deliverable )= (SELECT COUNT( * )
FROM passed_deliverable), '_TRUE_','_FALSE_') AS theTest
Wrap up the query in php and do simple logic.
???
Upvotes: 2
Reputation: 74219
"Wow, it worked. thanks for the answer Mr. @Fred-ii- – Mark Yu"
You're presently assigning =
instead of comparing ==
with if ($row2=$row3)
Change that to if ($row2==$row3)
.
Upvotes: 3
Reputation: 108460
This looks highly inefficient from the view of the database, returning a set of rows, containing every value in the row, so you can get a count? Why not let the database return just the count. And rather than doing that in two separate queries, why not just return those counts in the same query?
You could run just one query:
SELECT ( SELECT COUNT(1)
FROM deliverable
WHERE deliverable_category_id=3
) AS cnt_deliverable
, ( SELECT COUNT(1)
FROM passed_deliverable
WHERE deliverable_category_id=3
) AS cnt_passed_deliverable
And then do the comparison of the two values returned.
And for the love of all that is good and beautiful in this world... please just STOP using the mysql interface. It's deprecated. New development should be using mysqli or PDO.
Why isn't the code checking the return from the query, to verify that it executed without error? Or, we just want to let a function we call on an invalid query handle die.
Stop the insanity.
Just STOP.
Upvotes: 2
Reputation: 11
$num_rows1 = mysql_num_rows(mysql_query(SELECT * FROM deliverable WHERE deliverable_category_id=3));
$num_rows2 = mysql_num_rows(mysql_query(SELECT * FROM passed_deliverable WHERE deliverable_category_id=3));
if($num_rows1 != $num_rows2)
echo "not equal";
Upvotes: 1