0

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.

4
  • So you want list of images which are present in db_special_image or NOT present in db_special_image? Commented Apr 10, 2017 at 6:01
  • yes.I need query for that. Commented Apr 10, 2017 at 6:03
  • I am asking, are you comparing the image names Like if 123.png with subcatid 60 is present in both tables then your query should show that row as result. Correct? Commented Apr 10, 2017 at 6:08
  • @Hmmm : Yes,you are correct. Commented Apr 10, 2017 at 6:12

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

3 Comments

Haven't tested it, can you please check and let me know if any issue
Ok,let me to test and let you know.
Welcome, poke if any other hurdles.
0

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";

Comments