php数组:如何对查询使用不同的值

时间:2019-03-14 14:56:34

标签: php mysql

我有此代码:

$sql='SELECT id FROM table1 WHERE "'.$date.'"=Date';
$result = mysqli_query($connect, $sql) or die(mysqli_error($connect));;
while($row = mysqli_fetch_array($result)){
    $c=$row['id'];
}

这将返回多个值。如何将不同的值用于另一个查询? 例如:

$sql2='SELECT name FROM TABLE2 WHERE name="'.$c.'" ';

PS:我知道代码不好

2 个答案:

答案 0 :(得分:0)

这是您的意思吗?不确定100%,但希望对您有所帮助...

$sql='SELECT id FROM table1 WHERE Date = '.$date;
$result = mysqli_query($connect, $sql) or die(mysqli_error($connect));;
while($row = mysqli_fetch_array($result)){
    $sql2='SELECT name FROM TABLE2 WHERE name="'.$row['id'].'" ';
    $sub_result = mysqli_query($connect, $sql2) or die(mysqli_error($connect));
}

答案 1 :(得分:0)

我不确定这是否是您所需要的,但是我们开始:

$sql='SELECT id FROM table1 WHERE Date="'.$date.'"';
$result = mysqli_query($connect, $sql) or die(mysqli_error($connect));;
while($row = mysqli_fetch_array($result)){
    $c=$row['id'];

    $sql2='SELECT name FROM TABLE2 WHERE name="'.$c.'" ';
    //DO SOMETHING HERE
}

或使用LEFT JOIN

$sql='SELECT table1.something, TABLE2.something2 
      FROM table1
      LEFT JOIN TABLE2  
      ON table1.id=TABLE2.name

      WHERE table1.Date="'.$date.'"';
$result = mysqli_query($connect, $sql) or die(mysqli_error($connect));;
while($row = mysqli_fetch_array($result)){
    //DO SOMETHING HERE
}