比较/检查MySQL中的两个表数据

时间:2013-04-10 09:54:13

标签: php mysql

我需要一些关于检查数据的帮助。我有两张桌子。我想检查tableA中的内容中是否包含tableB中的file_name。如果没有,那么结果是file_name是这个。

select * from tableA;
| id | file_name
+---+----------
|  1 | apple.jpg 
|  2 | green_apple.jpg
|  3 | red_apple.jpg
|  4 | apple1.jpg
|  5 | apple-juce.jpg

////////////////////////////////////////////

select * from tableB;

| id |  title   |content 
+----+----------+--------
|  1 |  title1  | <img style="border:1px dotted;width:463px;height:611px;margin-left:20px;float:right;padding:3px;" src="image/green_apple.jpg" /><p>Lorem Ipsum is simply <b>dummy text</b> of the printing and typesetting industry. </p>
|  2 |  title2  | <p>Lorem Ipsum is simply <b>dummy text</b> of the printing and typesetting industry. </p><img style="border:1px dotted;width:463px;" src="image/apple.jpg" />
|  3 |  title3  | <img style="border:1px dotted;width:463px;" src="image/apple.jpg" /><p>Lorem Ipsum is simply <b>dummy text</b> of the printing and typesetting industry. </p><img style="border:1px dotted;width:463px;" src="image/apple.jpg" />
|  4 |  title4  | <img style="border:1px dotted;width:463px;" src="image/red_apple.jpg" /><p>Lorem Ipsum is simply <b>dummy text</b> of the printing and typesetting industry. </p><img style="border:1px dotted;width:463px;" src="image/apple.jpg" />
|  5 |  title5  | <p>Lorem Ipsum is simply <b>dummy text</b> of the printing and typesetting industry. </p><img style="border:1px dotted;width:463px;" src="image/apple1.jpg" />

1 个答案:

答案 0 :(得分:2)

我无法得到你想要的东西..但是这个查询可能对你有帮助..

首先,您在tableA中有文件名,该文件名包含在tableB的内容中。因此,首先您要搜索该名称是否存在...之后您可以使用NOT IN来选择您想要的数据(如果不存在于tableB中)。

    "select file_name from tableA where file_name like '%$search%' 
    AND file_name NOT IN (select content from tableB where file_name like '%$search%'); 
    ";