我试图将一个SQL语句的输出转换为另一个SQL语句,尽管它似乎不起作用。有替代方案,我将如何使用它?
$id = ($_SESSION["user_id"]);
$query1 = "SELECT event_id FROM booking where user_id = '$id'";
$result = mysql_query($query1);
$query2 = "SELECT * FROM event where event_id = '$result'";
$results = mysql_query($query2);
答案 0 :(得分:3)
您可以加入表格,尝试使用以下语法
SELECT event.event_id,booking.*
FROM event
JOIN booking on event.event_id=booking.event_id
WHERE booking.user_id='$id';
答案 1 :(得分:0)
使用一个查询而不是两个查询是什么:
$id = ($_SESSION["user_id"]);
$query2 = "SELECT * FROM event
where event_id IN (SELECT event_id
FROM booking
where user_id = '$id')";
$results = mysql_query($query2);
答案 2 :(得分:0)
您也可以使用sub-query在单个查询中执行此操作,如下所示;
$id = $_SESSION["user_id"];
$query = "SELECT * FROM event where event_id = (SELECT event_id FROM booking where user_id = '$id' LIMIT 1)";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
这只是一个db调用,而不是两个,因此效率更高。