在Another Mysql Command中使用一个Mysql命令的结果?

时间:2009-08-15 19:34:09

标签: php mysql

请原谅我的无知,但我很难搞清楚这一点。

我正在尝试从一个mysql命令获取结果,并在另一个命令中使用它。

这是我的代码,它不起作用。

//select the event end date of event ID
$sql = "SELECT enddate FROM mm_eventlist_dates WHERE id = $id";
$result = mysql_query($sql);

//plug in the event end date, find event that starts the next day
$sql = "SELECT id FROM mm_eventlist_dates WHERE startdate = date_add($result, INTERVAL 1 DAY)";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
echo "Next Event ID" . $row['id'];

我迷路了。

请帮忙!

谢谢,尼克

7 个答案:

答案 0 :(得分:2)

如果我理解您要完成的任务,看起来您想要查找在给定事件后一天开始的所有事件。正确?在这种情况下,您要做的是自联接,即将表连接到自身。您需要为表至少出现一个别名,以便SQL可以区分它们。

所以也许是这样的:

SELECT e2.id
FROM mm_eventlist_dates e1
join mm_eventlist_dates e2 on e2.startdate = date_add(e1.enddate, INTERVAL 1 DAY)
where e1.id=$id

答案 1 :(得分:1)

是否有理由不能将它们合并到一个查询中?

SELECT m1.id FROM mm_eventlist_dates m1  
JOIN mm_eventlist_dates m2 ON m1.startdate = date_add(m2.enddate, INTERVAL 1 DAY)  
WHERE m2.id = $id

答案 2 :(得分:0)

mysql_query()返回结果集,而不是实际的数据库项。要做你想做的事情,做一些类似的事情(不包括错误检查等):

//select the event end date of event ID
$sql = "SELECT enddate FROM mm_eventlist_dates WHERE id = $id";
$result = mysql_query($sql);

$enddateRow = mysql_fetch_array($result);

//plug in the event end date, find event that starts the next day
$sql = "SELECT id FROM mm_eventlist_dates WHERE startdate = date_add('" . $enddateRow["enddate"] . "', INTERVAL 1 DAY)";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
echo "Next Event ID" . $row['id'];

答案 3 :(得分:0)

您不能直接在date_add中使用$ result。调用mysql_fetch_array(稍后你会做几行),并使用$ row ['enddate']。

答案 4 :(得分:0)

//select the event end date of event ID
$sql = "SELECT enddate FROM mm_eventlist_dates WHERE id = $id";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$enddate = $row['enddate'];

//plug in the event end date, find event that starts the next day
$sql = "SELECT id FROM mm_eventlist_dates WHERE startdate = date_add($enddate, INTERVAL 1 DAY)";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
echo "Next Event ID" . $row['id'];

我认为

答案 5 :(得分:0)

您无法在另一个查询中直接使用mysql_query的结果,您需要先获取该值。

而不是

$result = mysql_query($sql);

//plug in the event end date, find event that starts the next day
$sql = "SELECT id FROM mm_eventlist_dates WHERE startdate = date_add($result, INTERVAL 1 DAY)";

尝试

   $result = mysql_query($sql);
   $enddate = mysql_fetch_assoc($result);

    //plug in the event end date, find event that starts the next day
    $sql = "SELECT id FROM mm_eventlist_dates WHERE startdate = date_add($enddate, INTERVAL 1 DAY)";

答案 6 :(得分:0)

试试这个

//select the event end date of event ID
$sql = "SELECT enddate FROM mm_eventlist_dates WHERE id = $id";
$result = mysql_query($sql);

$row = mysql_fetch_assoc($result)
//plug in the event end date, find event that starts the next day
$sql = "SELECT id FROM mm_eventlist_dates WHERE startdate = date_add(".$row['enddate'].", INTERVAL 1 DAY)";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
echo "Next Event ID" . $row['id'];