0

我的数据库中有这些表:

表A:

id | haystack
-------------
1  | 682,401
2  | 351

表 B:

id | needle
-------------
1  | 352
2  | 682
3  | 978

我要做的就是检查表 A 中的干草堆是否包含表 B 中的任何针头。我在 PHP 中执行了以下代码:

$res_A = mysql_query('SELECT * FROM table_A');
while($row_A = mysql_fetch_array($res_A)){
    $res_B = mysql_query('SELECT * FROM table_B');
    while($row_B = mysql_fetch_array($res_A)){
        if(strlen(strstr($row_A['haystack'], $row_B['needle']) > 0)){
            echo 'I found this needle: '.$row_B['needle'].' in this haystack: '.$row_A['haystack'].'<br />';
        }
    }
}

但是,它不起作用。我试图弄清楚一整天,但没有机会。我需要提到干草堆和针列是 Varchars。

你能帮我解决这种情况吗?提前致谢!

4

3 回答 3

1

注意你的括号放置在strlen(strstr($row_A['haystack'], $row_B['needle']) > 0)

尝试:if(strlen(strstr($row_A['haystack'], $row_B['needle'])) > 0)

您正在> 0调用strlen

于 2011-07-19T13:58:53.623 回答
1

为什么不将其全部保存在 SQL 服务器上?更容易和更快..

SELECT * from hay, needle WHERE hay.haystack LIKE CONCAT('%',needle.needle,'%');

于 2011-07-19T14:01:09.537 回答
0

试试这个:

$res_A = mysql_query('SELECT haystack FROM table_A');
while($row_A = mysql_fetch_array($res_A)){
    $res_B = mysql_query("SELECT needle FROM table_B WHERE needle = '" . $row_A['haystack'] . "'");
    if(mysql_num_rows($res_B) > 0) {  
            echo 'I found this needle: '.$row_B['needle'].' in this haystack: '.$row_A['haystack'].'<br />';
    }
}
于 2011-07-19T13:57:53.593 回答