user5443928
user5443928

Reputation:

How to compare two tables using PHP and MySQL

I need one help. I need to check one table's column data is present in another table in same database using PHP and MySQL.I am explaining my table below.

db_gallery:

id       subcat_id         image

1           60             123.png

2           60             234.png

3           58             456.png

db_special_image

id      subcat_id         name        gallery_image

 1         60              aaa          123.png

 2         58              bbb          456.png

Here I need to check whether any gallery image is present inside the db_special_image table. I need to check with subcat_id. Suppose I know the subcat_id=60. I need to check any image from db_gallery table belongs to subcat_id=60 is present in db_special_image table or not. If any image is there then it will return 1 otherwise 0. I need query for this. Please help me.

Upvotes: 0

Views: 444

Answers (3)

JYoThI
JYoThI

Reputation: 12085

Use mysql INNER JOIN The INNER JOIN keyword selects records that have matching values in both tables. ON db_gallery.subcat_id = db_special_image.subcat_id

SELECT name,db_gallery.subcat_id,image from db_gallery INNER JOIN db_special_image ON db_gallery.subcat_id = db_special_image.subcat_id

if num_rows count is more than 0 echo "1";

else echo "0";

Upvotes: 0

Hmmm
Hmmm

Reputation: 562

To show images which are present in db_gallery as well as db_special_image...

Bad Inner Query?

SELECT * from db_gallery WHERE db_gallery.image IN (SELECT gallery_image FROM db_special_image WHERE db_gallery.subcat_id = db_special_image.subcat_id)

Join

SELECT * from db_gallery INNER JOIN db_special_image ON db_gallery.subcat_id = db_special_image.subcat_id AND db_gallery.image=db_special_image.gallery_image

Upvotes: 0

Akshay N Shaju
Akshay N Shaju

Reputation: 355

You can use Mysql INNER JOIN to JOIN two tables . then use count('something'); if it's !=0, echo "1"; else echo "0"

See this tutorial

Upvotes: 1

Related Questions