为什么此查询无法正常工作

时间:2016-10-19 06:25:40

标签: mysql sql

我正在尝试根据用户团队显示搜索结果。只有当我按客户名称搜索但是当我通过home_phone搜索时它才显示其他团队的记录

$query = "SELECT COUNT(*) as num FROM $tableName WHERE team = '".$team."' AND customer_name LIKE '%".$srcquery."%' OR home_phone LIKE '%".$srcquery."%' OR alt_phone LIKE '%".$srcquery."%' ORDER BY id DESC";
    $total_pages = mysql_fetch_array(mysql_query($query));
    $total_pages = $total_pages[num];   

    // Get page data
    $query1 = "SELECT * FROM $tableName WHERE team = '".$team."' AND customer_name LIKE '%".$srcquery."%' OR home_phone LIKE '%".$srcquery."%' OR alt_phone LIKE '%".$srcquery."%' ORDER BY id DESC";
    $result = mysql_query($query1);

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

AND之后使用括号:

 $query = "SELECT COUNT(*) as num FROM $tableName WHERE team = '".$team."' AND (customer_name LIKE '%".$srcquery."%' OR home_phone LIKE '%".$srcquery."%' OR alt_phone LIKE '%".$srcquery."%') ORDER BY id DESC";

注意:所有OR语句都应在括号内。