【问题标题】:Compare the results of two tables using foreach使用 foreach 比较两个表的结果
【发布时间】:2013-04-30 03:02:23
【问题描述】:

我在数据库中有两个表,我想使用 ist 表的结果并将其与第二个表进行比较,例如:

// we connect to example.com and port 3307
mysql_connect("localhost", "root", "pass123") or die(mysql_error()); 

mysql_select_db("PhGateway") or die(mysql_error()); 
$result = mysql_query("select mtMsgId from SMS where SMS.`result` = '0' ");

while($row = mysql_fetch_array($result))
{

$mtMsgid=$row['mtMsgId'];

}

我想将$mtMsgid的结果与另一个表进行比较然后显示 另一个表名是 DN,有两个字段 mtMsgIdmsgStatus 喜欢:

select * from DN where mtMsgId = 'the whole above result'

【问题讨论】:

  • 1.使 $mtMsgid 成为一个数组。 2.使用IN():'select * from DN where mtMsgId IN('.implode(',', $mtMsgid).')' 3.或者使用JOIN

标签: php mysql loops foreach


【解决方案1】:

我认为您正在寻找 JOIN。您可以在 SQL 中执行此操作:

$result = mysql_query("select s.mtMsgId,j.msgStatus from JN j, SMS s WHERE s.mtMsgId = j.mtMsgId AND s.result = '0' ");

这命名了两个要从中获取数据的表,“SMS”为 s,“JN”为 j。您将结果与 s.mtMsgId = j.mtMsgId 进行“同步”(根据他们的 mtMsgIds 将它们结合起来),并且您对 SMS.result 为 0 的结果感兴趣。

【讨论】:

  • 不用担心。如果它解决了您的问题,请接受答案。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多