MySQL在查询中运行查询

时间:2012-10-08 09:38:01

标签: php mysql

我有一个查询,获得5行数据,如下面的示例

$query = "SELECT ref,user,id FROM table LIMIT 0, 5"; 
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
}

我想在每个结果中运行查询,如下所示

$query = "SELECT ref,user,id FROM table LIMIT 0, 5"; 
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
 $ref = $row['ref'];
$query = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) )
{
$title = $row['title'];
} else {
$title = "No Title";
}
echo "$ref - $tile";
}

但由于某种原因,当我在其中添加查询时,它只显示第一行。我似乎可以让它运行所有5个查询。

8 个答案:

答案 0 :(得分:2)

SELECT 
    t1.ref, 
    t1.user,
    t1.id,
    t2.domain,
    t2.title
FROM 
    table AS t1
    LEFT JOIN anothertable AS t2 ON
        t2.domain = t1.ref
LIMIT
    0, 5

答案 1 :(得分:2)

问题是在while循环中你使用相同的变量$result,然后被覆盖。在while循环中为$result使用另一个变量名。

答案 2 :(得分:1)

您在while循环中更改$ query的值。 将变量名称更改为其他名称。

例如:

$query = "SELECT ref,user,id FROM table LIMIT 0, 5"; 
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
 $ref = $row['ref'];
$qry = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$rslt = mysql_query($qry) or die(mysql_error());
if (mysql_num_rows($rslt) )
{
$title = $row['title'];
} else {
$title = "No Title";
}
echo "$ref - $tile";
}

答案 3 :(得分:1)

使用以下内容:

$query = "SELECT ref,user,id FROM table LIMIT 0, 5"; 
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
    $ref = $row['ref'];
    $query_domain = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
    $result_domain = mysql_query($query_domain) or die(mysql_error());
    if (mysql_num_rows($result_domain) )
    {
        $row_domain = mysql_fetch_row($result_domain);
        $title = $row_domain['title'];
    } else {
        $title = "No Title";
    }
    echo "$ref - $title";
}

答案 4 :(得分:1)

这是一个逻辑问题。它就是这样,因为你在循环内外都是相同的变量名。

<强>解释

$query = "SELECT ref,user,id FROM table LIMIT 0, 5"; 
$result = mysql_query($query) or die(mysql_error());
// Now $results hold the result of the first query

while($row = mysql_fetch_array($result))
{
    $ref = $row['ref'];

    //Using same $query does not affect that much
    $query = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";    

    //But here you are overriding the previous result set of first query with a new result set
    $result = mysql_query($query) or die(mysql_error());
    //^ Due to this, next time the loop continues, the $result on whose basis it would loop will already be modified

//..............

解决方案1:

避免对内部结果集使用相同的变量名

$query = "SELECT ref,user,id FROM table LIMIT 0, 5"; 
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result))
{
    $ref = $row['ref'];
    $query = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";    
    $sub_result = mysql_query($query) or die(mysql_error());
    // ^ Change this variable so that it does not overrides previous result set

解决方案2: 避免双重查询情况。使用联接在一次查询调用中获取数据。 (注意: 您应该始终尝试优化查询,以便最大限度地减少服务器上的查询数量。

SELECT 
    ref,user,id 
FROM 
    table t
INNER JOIN 
    anothertable t2 on t.ref t2.domain
LIMIT 0, 5

答案 5 :(得分:0)

了解SQL joins

SELECT table.ref, table.user, table.id, anothertable.title
FROM   table LEFT JOIN anothertable ON anothertable.domain = table.ref
LIMIT  5

答案 6 :(得分:0)

您正在更改循环中$result的值。更改第二个查询以使用其他变量。

答案 7 :(得分:0)

由于您使用了两次相同的名称,因此没有给出正确的结果,使用不同的名称,如此编辑。

$query = "SELECT ref,user,id FROM table LIMIT 0, 5"; 
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
    $ref = $row['ref'];
    $query1 = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
    $result1 = mysql_query($query1) or die(mysql_error());
    if (mysql_num_rows($result1) )
    {
        $title = $row['title'];
    } else {
        $title = "No Title";
    }
    echo "$ref - $tile";
}