Php sql查询工作正常,但因为我在同一个连接中使用两个条件不起作用

时间:2016-08-05 04:25:16

标签: php mysql

这是我的查询运作良好,但想在其中添加另一个条件,但它给出错误不唯一的表/别名:'用户'

这是我的代码

$query = 'select group_concat(DISTINCT evaluation_section. section_name) as   allowed_sections from evaluation_section 
            join section_permissions on (section_permissions.section_id = evaluation_section.section_id )
            join users on ( users.user_id = section_permissions.user_id )
            join users on ( users.user_id = section_permissions.assigned_for )
            where  users.user_id = '.$user['user_id'];

还希望从用户表中选择名称,然后在(users.user_id = section_permissions.assigned_for )

上使用此条件加入用户

请帮帮我

这是我编辑的文字
这是我完整的工作查询

$q = 'select  group_concat(DISTINCT evaluation_section. section_name) as   allowed_sections from evaluation_section 
        join section_permissions on (section_permissions.section_id = evaluation_section.section_id )
        join users on ( users.user_id = section_permissions.user_id )
        where users.user_id = '.$user['user_id'];

但我希望它从users表中选择名称,然后将其与users.user_id = section_permissions.assigned_for匹配,以便获得assign_for id name的名称 和group_concat之前一样,我使用users.name然后它的工作,但它显示名称users.user_id = section_permissions.user_id,但我想显示er_id的名称)             users.user_id ='。$ user [' user_id'];

但我希望它从users表中选择名称,然后将其与users.user_id = section_permissions.assigned_for匹配 在同一个查询中,因为其中两个不起作用

1 个答案:

答案 0 :(得分:3)

尝试这样做:

$query = 'select group_concat(DISTINCT evaluation_section. section_name) as   allowed_sections from evaluation_section 
        join section_permissions on (section_permissions.section_id = evaluation_section.section_id )
        join users u1 on ( u1.user_id = section_permissions.user_id )
        join users u2 on ( u2.user_id = section_permissions.assigned_for )
        where  u1.user_id = '.$user['user_id'];  

因为您正在加入"用户"表格两次,你必须为它们提供唯一的别名。