= '$todaysdate' ";-6ren">
gpt4 book ai didi

php - MySQL - 基于另一个查询的查询

转载 作者:行者123 更新时间:2023-11-29 03:55:33 24 4
gpt4 key购买 nike

我写了很多类似于下面的查询示例代码的查询。我想知道是否有更高效的代码/脚本?

$query1 ="SELECT * FROM table1 WHERE date >= '$todaysdate' ";
$result1 = mysql_query($query1)
or die ("Error in query: $query1. " . mysql_error());
if (mysql_num_rows($result1) > 0) {
while($row1 = mysql_fetch_object($result1)) {

echo "$row1-date";

$query2 ="SELECT * FROM table2 WHERE table1ID >= '$row1-table1ID' ";
$result2 = mysql_query($query2)
or die ("Error in query: $query2. " . mysql_error());
if (mysql_num_rows($result2) > 0) {
while($row2 = mysql_fetch_object($result2)) {
echo "$row->datatable2";
}
}
}
}

最佳答案

尝试使用 SQL JOINs ,就像下面的例子:

SELECT 
*
FROM
table1
INNER JOIN
table2 ON (table2.table1ID = table1.ID)
WHERE
table1.date >= '2009-12-20';

关于php - MySQL - 基于另一个查询的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1935634/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com