简体   繁体   English

在后面的 select 中使用之前 sql select 的一部分

[英]Using part of a previous sql select in a later select

So I am trying to use part of the results of a select statement in a second and third select after the results have been set to a variable因此,在将结果设置为变量后,我试图在第二个和第三个 select 中使用 select 语句的部分结果

$result = $db->query('SELECT operator, COUNT(DISTINCT dupi), COUNT(DISTINCT userID), dupi FROM reported GROUP BY operator ORDER BY COUNT(DISTINCT userID) DESC');

foreach($result as $row)
{
  $result2 = $db->query('SELECT dupi FROM reported WHERE operator = $row['operator']');

  $result3 = $db->query('SELECT dupi FROM reported WHERE operator = $row['operator'] AND dupi != NULL');

I don't know if this is possible but I am wanting to do this so I can do an if statement later in the code我不知道这是否可行,但我想这样做,以便稍后在代码中执行 if 语句

Try this:尝试这个:

$result = $db->query('SELECT operator, COUNT(DISTINCT dupi), COUNT(DISTINCT userID), dupi FROM reported GROUP BY operator ORDER BY COUNT(DISTINCT userID) DESC');

foreach($result as $row)
{
    $result2 = $db->query("SELECT dupi FROM reported WHERE operator = " . $row['operator']);
    $result3 = $db->query("SELECT dupi FROM reported WHERE operator = " . $row['operator'] . " AND dupi != NULL");
}

If it works it was just a concatenation problem with 'SELECT...$row['如果它有效,那只是'SELECT...$row['的串联问题

One way to debug this kind of problem:调试此类问题的一种方法:

foreach($result as $row)
{
    // You write the query in a string
    $sql2 = "SELECT dupi FROM reported WHERE operator = " . $row['operator'];

    var_dump($sql2); // Check how it looks

    $result2 = $db->query($sql2);

    // same for $result3
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM