简体   繁体   English

如何使用PHP和MySQL比较两个表

[英]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. 我需要使用PHP和MySQL检查同一数据库的另一个表中是否存在一个表的列数据。我在下面解释我的表。

db_gallery: db_gallery:

id       subcat_id         image

1           60             123.png

2           60             234.png

3           58             456.png

db_special_image 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. 在这里,我需要检查db_special_image表中是否存在任何图库图像。 I need to check with subcat_id . 我需要检查subcat_id Suppose I know the subcat_id=60 . 假设我知道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. 我需要检查db_gallery表中是否有任何属于subcat_id=60是否存在于db_special_image表中。 If any image is there then it will return 1 otherwise 0. I need query for this. 如果有任何图像,则它将返回1,否则返回0。我需要对此进行查询。 Please help me. 请帮我。

You can use Mysql INNER JOIN to JOIN two tables . 您可以使用Mysql INNER JOIN来联接两个表。 then use count('something'); 然后使用count('something'); if it's !=0, echo "1"; else echo "0"

See this tutorial 请参阅本教程

To show images which are present in db_gallery as well as db_special_image... 显示db_gallery和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

Use mysql INNER JOIN The INNER JOIN keyword selects records that have matching values in both tables. 使用mysql INNER JOIN INNER JOIN关键字选择在两个表中具有匹配值的记录。 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"; 如果num_rows计数大于0,则echo "1";

else echo "0"; 否则echo "0";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM