我试图在第二个查询中将一个查询的结果用作WHERE的一部分。我不确定如何最好地解决这个问题,非常感谢任何帮助:
第一次查询:
$result = mysql_query("SELECT `keyword` FROM `history` ORDER BY `last_update` ASC LIMIT 1 ");
while($row = mysql_fetch_array($result))
$keyword = $row['keyword'];
第二次查询
$result = mysql_query("SELECT `id` FROM `data_store` WHERE `keyword`= [result from my first query] ORDER BY `id` DESC LIMIT 1");
while($row = mysql_fetch_array($result))
$id = $row['id'];
为了澄清,keyword.data_store与keyword.history
相关答案 0 :(得分:1)
使用子查询
$result = mysql_query("
SELECT `id`
FROM `data_store`
WHERE `keyword` = (SELECT `keyword`
FROM `history`
ORDER BY `last_update` ASC
LIMIT 1)
ORDER BY `id` DESC");
while($row = mysql_fetch_array($result))
$id = $row['id'];
答案 1 :(得分:0)
$result = mysql_query("SELECT `id`
FROM `data_store`
WHERE `keyword` = (
SELECT `keyword`
FROM `history`
ORDER BY `last_update` ASC
LIMIT 1 )
ORDER BY `id` DESC LIMIT 1");
答案 2 :(得分:0)
(剥离PHP方面因为它无关紧要)
SELECT `id` FROM `data_store` WHERE
`keyword` IN (
SELECT `keyword` FROM `history` ORDER BY `last_update` ASC LIMIT 1
)
ORDER BY `id` DESC LIMIT 1