MASS WHERE / IN语句,如何处理空/返回数据以及匹配?

时间:2013-03-20 17:37:24

标签: php mysql match where empty-list

我有一个问题,希望你能帮助我......

我正在使用WHERE / IN执行一个大型SELECT语句,并为所有搜索条件传入$数组'使用。

快速示例..(虽然最后$ array更大)

$harNumArray = (0100001943,0100001944,0100002392,0100007414,0100012110,0100015761,0100015835);
$harNumArray2 = implode(',', $harNumArray);
$results  = mysqli_query($mysqli, "SELECT har_id, guar_num FROM placements WHERE har_id IN ($harNumArray2)");

//**outputting the matches values/data from the search (select)**

while ($row = mysqli_fetch_assoc($results)) {
    //echo "HAR_ID: ". $row["har_id"]. "\tGUAR_NUM: " . $row["guar_num"] . "\r\n<BR>";
    echo $row["har_id"]. "\t" . $row["guar_num"] . "\r\n<BR>";
    fwrite($fh, $row["har_id"] . "\t" . $row["guar_num"] . "\r\n");
} 

这很好/很好(*感谢msturdy提示)......&#39;当&#39;有一个匹配..如果没有匹配..没有返回。

但是..例如,如果在数据库中找不到$ harNumArray(索引0)中的第一个值... $ row = mysqli_fetch_assoc中没有返回任何内容($ results循环...

仅查找数字..以及从中提取数据的匹配/关联数据..

我希望它仍然像这样:

  • 0100001943 ^ - //数据库中缺少/热找到的号码(条目)但仍然如此 在第一个位置使用原始搜索词)
  • 0100001944 ^ 0123467894
  • 0100002392 ^ 0011122234
  • 0100007414 ^ 0002130567
  • 0100012110 ^ 0000045432
  • 0100015761 ^ 0001000045
  • 0100015835 ^ 0998775233

或者这样可以接受(但是上面的布局会更好):

  • - ^ - // DB中缺少/热找到的号码(条目)
  • 0100001944 ^ 0123467894
  • 0100002392 ^ 0011122234
  • 0100007414 ^ 0002130567
  • 0100012110 ^ 0000045432
  • 0100015761 ^ 0001000045
  • 0100015835 ^ 0998775233

如果在WHERE / IN SELECT中找不到$ harNumArray数组中的第一个数字..它不会返回任何内容(这是有意义的)..

但是......我需要&#39;考虑不匹配..并插入一个空白(或其他东西)..以保持最终“#”列表的间距/顺序。 (文本文件)

我该怎么做呢?

谢谢!

修改::

这是当前的代码状态:(仍然不返回匹配项)

@Cal

//stack overflow approach:
    //format array data
    $harNumArray2 = "'" . implode("','", $harNumArray) . "'";    //single quotes
    //$harNumArray2 = implode(',', $harNumArray);  //no quotes
    //$harNumArray2 = '"' . implode('","', $harNumArray) . '"';  //double quotes
    //print_r("ARRAY CHECK: " . $harNumArray2);

    $results = mysqli_query($mysqli, "SELECT har_id, guar_num FROM placements WHERE har_id IN ($harNumArray2) ORDER BY har_id ASC") or die(mysql_error());
    //$results = mysqli_query($mysqli, "SELECT har_id, guar_num FROM placements WHERE har_id IN (" . $harNumArray2 . ") ORDER BY har_id ASC") or die(mysql_error());

    echo("<BR>");
    print_r("SELECT har_id, guar_num FROM placements WHERE har_id IN ($harNumArray2)");
    echo("<BR>");

    $rows = array();
    while($row = mysqli_fetch_assoc($results)) {
        $rows[$row['har_id']] = $row;
    }
    foreach ($harNumArray as $id){
        if (isset($rows[$id])){
            //... do something with $rows[$id]
            print_r($rows[$id] . "<BR>");

        }else{
            //... no match for $id
            print_r("....no match....");
            echo("<BR>");
        }
    }

仍然没有返回匹配...无论是单引号,没有引号还是双引号..

难倒?

感谢

更新II:

@Cal -

好的,我运行了你提供的代码..

这是我的输出:

数组([har_id] =&gt; 000100007537 [guar_num] =&gt; 0000676798)数组([har_id] =&gt; 000100007538 [guar_num] =&gt; 0000676798)数组([har_id] =&gt; 000100007539 [guar_num] = &gt; 0000676798)数组([har_id] =&gt; 000100007768 [guar_num] =&gt; 0000675266)数组([har_id] =&gt; 000100007769 [guar_num] =&gt; 0000675266)

DB = varchar(12)中的HAR_ID

DB = varchar(12)中的GUAR_NUM

如果我在#results查询中留下引号。我得到数据返回..(但无论如何......没有找到匹配项)

1 个答案:

答案 0 :(得分:0)

执行查询:

$harNumArray = array(
    '0100001943',
    '0100001944',
    '0100002392',
    '0100007414',
    '0100012110',
);
$harNumArray2 = "'".implode("','", $harNumArray)."'";
$results = mysqli_query($mysqli, "SELECT har_id, guar_num FROM placements WHERE har_id IN ($harNumArray2)");

然后构建结果地图:

$rows = array();
while ($row = mysqli_fetch_assoc($results)) {
    $rows[$row['har_id']] = $row;
}

然后你可以循环检查匹配的ID:

foreach ($harNumArray as $id){

    if (isset($rows[$id])){
        ... do something with $rows[$id]
    }else{
        ... no match for $id
    }
}

更新1:

听起来你的查询没有返回任何内容。运行以下代码:

$results = mysqli_query($mysqli, "SELECT har_id, guar_num FROM placements LIMIT 5");
while ($row = mysqli_fetch_assoc($results)) print_r($row);

然后用输出更新你的问题。听起来您的数据库并不包含您的想法。