Finding/searching for missing values in MySQL

前端 未结 5 1101
清歌不尽
清歌不尽 2021-01-16 14:04

I am using MySQL and have a table called sales. Its primary key is sales_id.

-------------------------------------
sales_id | invo         


        
5条回答
  •  感动是毒
    2021-01-16 14:16

    As you are searching for invoice_id then make sure you have an index on that column. otherwise queries will be slow.

    You can try the following code

    $inv_ids=range(147,4497);
    $str_inv_ids=implode(",",$inv_ids);
    $query="SELECT DISTINCT invoice_id FROM sales WHERE invoice_id IN ($str_inv_ids)";
    $result=mysql_query($query);
    
    while ($row = mysql_fetch_assoc($result)) {
        $inv_ids_db[]=$row['invoice_id'];
    }
    // It gives invoice Ids that are in $inv_ids but not in $inv_ids_db
    $missing= array_diff($inv_ids,$inv_ids_db); 
    
    print_r($missing);
    

提交回复
热议问题