简体   繁体   English

如何使用php和mysql在一个表中搜索记录是否存在

[英]How to search for existence of record in one table from another using php and mysql

I use php and mysql. 我使用php和mysql。 I have two tables, 我有两张桌子

table A (Id: auto-increment , idno) table B (Id:auto-increment, sidno) . table A (Id: auto-increment , idno) table B (Id:auto-increment, sidno)

Table A contains about 3000 records and Table B contains about 27000 records. 表A包含约3000条记录,表B包含约27000条记录。 I want to search whether each of the records in table A exist in table B, if not print the records that does not exist in table B. 我想搜索表A中的每个记录是否存在于表B中,如果不打印的话,表B中不存在的记录。

I tried to retrieve the records in table A and checking them against table A, but I could not succeed. 我试图检索表A中的记录,并对照表A进行检查,但无法成功。 And it took a very long time to finished the query. 完成查询花费了很长时间。

And I have searched throughout but could not get something like this. 而且我搜索了整个内容,但找不到类似的内容。

Please can anybody help me. 请任何人能帮助我。

Thanks! 谢谢!

The following query might return all the idno which are not in table B 以下查询可能返回表B中没有的所有idno

 SELECT * FROM tableA WHERE `idno` NOT IN (SELECT `sidno` FROM tableB)

SQL Fiddle Demo SQL小提琴演示

Try this 尝试这个

   SELECT * FROM table2 t where sid NOT IN (select id from table1) ;

Demo 演示

like this 像这样

select * from tableA where Minus select id from where tableA.id=tableB.id; 从tableA中选择*,从负号中选择tableA.id = tableB.id;

MINUS http://www.techonthenet.com/sql/minus.php 减号 http://www.techonthenet.com/sql/minus.php

Ok. 好。 Try this: 尝试这个:

SELECT tableB.Id, tableB.sidno
FROM tableA
RIGHT JOIN tableB ON tableA.Id = tableB.ID
WHERE tableA.Id = 'NULL';

This should give you all the records you want. 这应该给您所有想要的记录。

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

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