我可以在此查询的where子句中使用多个条件吗?

时间:2014-07-17 10:36:20

标签: php sql wordpress

$sql="SELECT user_id, user_fname, user_lname, telephone, primary_email, dept_name, created_date, st_name, emp_level_name
            FROM ".$prefix."users_info, 
            ".$prefix."support_departments,
            /".$prefix."user_status,
            ".$prefix."company_user_levels
            WHERE ".$prefix."users_info.dept_id=".$prefix."support_departments.dept_id,
            ".$prefix."users_info.user_st=".$prefix."user_status.user_st,
            ".$prefix."users_info.user_level=".$prefix."company_user_levels.emp_level_id
            ORDER BY ".$sort_para." ";
            $employee_info = $wpdb->get_results($sql);

3 个答案:

答案 0 :(得分:0)

在WHERE语句中,用'#34;替换comas; AND":

$sql = "SELECT user_id, user_fname, user_lname, telephone, primary_email, dept_name, created_date, st_name, emp_level_name
FROM ${prefix}users_info, ${prefix}support_departments, ${prefix}user_status, ${prefix}company_user_levels
WHERE ${prefix}users_info.dept_id=${prefix}support_departments.dept_id AND ${prefix}users_info.user_st=${prefix}user_status.user_st AND ${prefix}users_info.user_level=${prefix}company_user_levels.emp_level_id
ORDER BY ".$sort_para;
$employee_info = $wpdb->get_results($sql);

答案 1 :(得分:0)

把" AND"放错了什么?或"或"条件之间的条款而不是逗号?

答案 2 :(得分:0)

您需要使用join来从多个表中获取数据,

FROM ".$prefix."users_info ui    
join  ".$prefix."user_status su on su.FOREIGNKEY = ui.PRIMARY

此外,在需要在每种情况之间使用AND或OR条件的情况下

WHERE ".$prefix."users_info.dept_id=".$prefix."support_departments.dept_id AND 
        ".$prefix."users_info.user_st=".$prefix."user_status.user_st