我將如何分辨其中一條記錄從下面的例子聯合查詢來區分內兩個查詢:工會
SELECT id, name, datetime FROM events
UNION
SELECT id, name, datetime FROM festival
ORDER BY datetime DESC
我使用它在PHP的方式是:
$stmt = $cxn->prepare('SELECT id, name, datetime FROM events WHERE id = ? UNION SELECT id, name, datetime FROM festival WHERE id = ? ORDER BY datetime DESC');
$stmt->bind_param('i', $event_id);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($id, $name, $datetime);
while ($stmt->fetch()) {
echo "ID: " . $id . "\n";
echo "Name: " . $name . "\n";
echo "Date: " . $datetime;
}
$stmt->close();
請幫我一把。
謝謝。
我如何以PHP的方式獲取該列,因爲我的綁定結果是? – Walter
@Aquillo糾正 – SajithNair
一個改進是使用'UNION ALL',因爲這些記錄不再匹配了 – Aquillo