A general single sql query

前端 未结 3 1488
醉话见心
醉话见心 2021-01-27 12:07

I have a table like this:

id | roll_no | name
---------------------
 1 |   111   | Naveed
 2 |   222   | Adil
 3 |   333   | Ali 

If I

相关标签:
3条回答
  • 2021-01-27 12:21

    Complete solution with the help of accepted answer.

    $tableName = "test"; 
    $fields = array( "id" , "roll_no" );
    $values = array( "1,111", "2,222" );
    
    $fieldsStr = implode(',', $fields);
    $valuesStr = implode("','", $values);
    
    // Get all records from remote table
    $sql = "SELECT * FROM $tableName WHERE concat_ws(',', $fieldsStr ) NOT IN ( '$valuesStr' )";
    
    0 讨论(0)
  • 2021-01-27 12:31
    select * from dummy where concat_ws (',', id, roll_no) not in ('1,111', '2,222')
    

    Complete solution:

    $tableName = "test"; 
    $fields = array( "id" , "roll_no" );
    $values = array( "1,111", "2,222" );
    
    $fieldsStr = implode(',', $fields);
    $valuesStr = implode("','", $values);
    $sql = "SELECT * 
        FROM $tableName 
        WHERE concat_ws(',', $fieldsStr ) NOT IN ( '$valuesStr' )";
    
    0 讨论(0)
  • 2021-01-27 12:37

    You will probably always have to explode the Array in PHP and pass the values as a string into the query (sprintf) so you probably can, and should, do all in PHP.

    One thing that catches my eye is that you are always using ID's. Are the ID's a unique or primary field? If so just forget about the roll_no as your query will be faster using just ID's.

    0 讨论(0)
提交回复
热议问题