我有疑问。我在一个查询本身中执行以下两个查询?
SELECT id FROM table1 WHERE time_stamp>=DATE_SUB(NOW(),INTERVAL 10 MINUTE) AND status<>200 ORDER BY time_stamp ASC LIMIT 2 "
和SELECT email FROM table2 WHERE wid='".$xy."'"
其中$xy
是从第一个选择查询中获取的ID。
我的PHP代码:
$dat =mysql_query("SELECT * FROM table1 WHERE time_stamp>=DATE_SUB(NOW(),INTERVAL 10 MINUTE) AND status<>200 ORDER BY time_stamp ASC LIMIT 2 ");
while($dt= mysql_fetch_assoc($dat)){
$add= mysql_query("SELECT email FROM table2 WHERE wid='".$dt['id']."'");
$result= mysql_fetch_assoc($add);
$to=$result['email'];
$subject="site down";
$message="your site is down";
$header="From:admin@gmail.com";
$retval=mail($to,$subject,$message,$header);}
我的表格如下所示:
表1:
id url status time_stamp
9 dgfgg 404 2012:09:05 13:04:56
10 gfh 404 0000:00:00 00:00:00
11 fg 200 0000:00:00 00:00:00
表2:
wid email
9 ufff@hguh
10 yfyff@hfg
11 yfyf@hg.com
任何人都可以建议一个查询来完成工作,而不是像我在PHP代码中那样编写两个吗?
答案 0 :(得分:-1)
SELECT email FROM table2 WHERE wid=(SELECT id FROM table1
WHERE time_stamp>=DATE_SUB(NOW(),INTERVAL 10 MINUTE)
AND status<>200 ORDER BY time_stamp ASC LIMIT 2)
Ooops,如果它返回多个id,则使用IN而不是=
$dat =mysql_query("SELECT email FROM table2 WHERE wid IN(SELECT id FROM table1
WHERE time_stamp>=DATE_SUB(NOW(),INTERVAL 10 MINUTE)
AND status<>200 ORDER BY time_stamp ASC)LIMIT 2 ");
while($result = mysql_fetch_assoc($dat)){
$to=$result['email'];
$subject="site down";
$message="your site is down";
$header="From:admin@gmail.com";
$retval=mail($to,$subject,$message,$header);}
是的,自PHP 5.5起,不推荐使用mysql函数,使用mysqli或PDO。
编辑好,尝试将LIMIT 2放在第一个括号
之外