mysqli_query选择with like子句

时间:2015-03-26 16:30:16

标签: php mysql

我必须阅读带有type,vers和variant(从EXCEL导入)的MySql表。使用三个字符串(例如:abc6zgqsdf)我必须通过表扫描并查明是否存在col1 = 'abc' AND col2 = '6zg' AND col3 = 'qsdf'的表条目。结果必须是“是有这样的条目”或“否”(布尔值)。 SELECT看起来怎么样? 我的代码根本不起作用!

    $result = mysqli_query($db, "SELECT *  FROM $table_name 
                        WHERE Type LIKE '%$typ%'
                        AND Version LIKE '%$version%' 
                        AND Variant LIKE '%$variant%'"); 

3 个答案:

答案 0 :(得分:0)

<强> SQL

SELECT CASE WHERE COL1='abc' AND COL2='def' AND COL3='ghi' THEN 'Yes there is such an entry' ELSE 'No' END as ColumnName from your_table

答案 1 :(得分:0)

$result = mysqli_query($db, "
SELECT IF ( EXISTS ( SELECT  *  FROM $table_name 
                        WHERE Type    LIKE '%$typ%'
                          AND Version LIKE '%$version%' 
                          AND Variant LIKE '%$variant%'
                   )
   'YES', 'NO')
";

无论满足条件的行数是多少,都会产生1行输出。此外,如果在所有3个条件中找到第一个匹配项,它将停止。

答案 2 :(得分:0)

以下代码有效:

$row = $mysqli->query("SELECT * FROM $table WHERE Typ LIKE '$typ' AND Vers LIKE '$vers' AND Var LIKE '$var'");
$result = $row->fetch_assoc();
if ($result == NULL) {
  echo "Not found"; }
else { 
 echo "Found!"; }