我有一个这样的表:
id | roll_no | name
---------------------
1 | 111 | Naveed
2 | 222 | Adil
3 | 333 | Ali
如果我有这样的数据:
$fields = array( "id" , "roll_no" )
and $values = array( "1,111", "2,222" );
这意味着我必须编写一个sql查询来从表中获取记录,其中(id != 1 and roll_no != 111)和(id != 2 and roll_no != 222)。这意味着将获取第三条记录。
如果我有这样的数据:
$fields = array( "id" )
and $values = array( "2", "3" );
这意味着我必须编写一个sql查询来从表中获取记录,其中(id!= 2)和(id!= 3)。这意味着将获取第一条记录。
Q:如何使用 php 编写一般的单个查询,以使用上述两个数据数组从表中获取数据。
Thanks
select * from dummy where concat_ws (',', id, roll_no) not in ('1,111', '2,222')
完整的解决方案:
$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' )";
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)