简体   繁体   English

IF语句中的MySQL SELECT WHERE

[英]MySQL SELECT WHERE within IF statement

I have a "group_names" table which stores user groups. 我有一个“ group_names”表,用于存储用户组。 Each group has a manager, which can be either an individual user, or be managed by another group. 每个组都有一个管理员,该管理员可以是单个用户,也可以由另一个组进行管理。 This is currently determined by group_names.mgr_type. 当前由group_names.mgr_type确定。 An INT value of 1 means that it's an individual user and a value of 2 means that it's a group that manages it. INT值1表示它是单个用户,值2表示它是管理它的组。

For example, the "General Users" group could be managed by the "IT Department" group or by "John Doe". 例如,“一般用户”组可以由“ IT部门”组或“ John Doe”来管理。 Unique ID fields on the users and group_names tables are "user_id" and "group_id". users和group_names表上的唯一ID字段是“ user_id”和“ group_id”。

I want to take the logic out of PHP and just put it in the SQL query, but I'm running into problems here. 我想从PHP中提取逻辑,仅将其放入SQL查询中,但是我在这里遇到了问题。 I'm not sure if this is possible given the table layout or if I'm just incapable of following simple instructions provided in the documentation. 我不确定在给定表格布局的情况下是否可行,或者我只是无法遵循文档中提供的简单说明。

SELECT gn.group_id, gn.group_name, ... gn.mgr_id, CONCAT(usr.user_firstname,' ',usr.user_lastname) AS created_name, usr.user_id,
CASE gn.mgr_type
    WHEN '1' THEN CONCAT(usr.user_firstname,' ',usr.user_lastname) WHERE usr.user_id=gn.mgr_id LIMIT 1
    WHEN '2' THEN gn.group_name WHERE gn.group_id=gn.mgr_id LIMIT 1
END AS mgr_name
FROM group_names gn 
LEFT JOIN users usr 
ON gn.created_by=usr.user_id 
ORDER BY group_name ASC;

I've tried different variations of IF and CASE, with and without parentheses, but keep getting the "invalid syntax" error. 我尝试了IF和CASE的不同变体(带或不带括号),但始终收到“无效语法”错误。

...for the right syntax to use near 'WHERE usr.user_id=gn.mgr_id LIMIT 1) WHEN '2' THEN (gn.group_name WHERE gn' at line 3

Can I even select a different row from the same table (select the group_name from a different row based on the mgr_id=group_id), or should I add another table? 我什至可以从同一张表中选择另一行(根据mgr_id = group_id从另一行中选择group_name),还是应该添加另一张表? Any suggestions on an easier/more efficient way to do this? 关于更简便/更有效的方法有什么建议吗?

Thanks! 谢谢!

I think you should try something like this(try shifting where in the join conditions of another instance of table): 我认为您应该尝试这样的事情(尝试移动另一个表实例的连接条件中的位置):

 SELECT gn.group_id, gn.group_name, ... gn.mgr_id,
        CONCAT(usr.user_firstname,' ',usr.user_lastname) AS created_name, 
        usr.user_id,
       CASE gn.mgr_type
         WHEN '1' THEN CONCAT(usr1.user_firstname,' ',usr1.user_lastname)
         WHEN '2' THEN gn1.group_name
      END AS mgr_name
 FROM group_names gn 
 LEFT JOIN users usr 
 ON gn.created_by=usr.user_id
 RIGHT OUTER JOIN users usr1
 ON usr1.user_id=gn.mgr_id LIMIT 1
 OUTER JOIN group_names gn1
 ON usr.user_id=gn1.mgr_id LIMIT 1
 ORDER BY group_name ASC;

Have you tried this since you already joined to those tables 您是否已经尝试过此操作,因为您已经加入了这些表

CASE gn.mgr_type
    WHEN '1' THEN CONCAT(mgr_usr .user_firstname,' ',mgr_usr .user_lastname)
    WHEN '2' THEN mgr_grp.group_name
END AS mgr_name

You actually need 2 more left joins for that to work 您实际上还需要2个左联接才能起作用

LEFT JOIN users mgr_usr 
   ON gn.mgr_id = mgr_usr.user_id 
LEFT JOIN group_names mgr_grp
   ON gn.mgr_id = mgr_grp.group_id

Left join to the user table to get the manager name if it's user or join to group_names table to retrieve the group name if it's a group 如果是用户,则左键联接到用户表以获取经理名称;如果是组,则左键联接到group_names表以检索组名称

You can restrict the JOINs further 您可以进一步限制JOIN

LEFT JOIN users mgr_usr 
   ON gn.mgr_id = mgr_usr.user_id AND gn.mgr_type = 1
LEFT JOIN group_names mgr_grp
   ON gn.mgr_id = mgr_grp.group_id AND gn.mgr_type = 2

try this: 尝试这个:

SELECT ...
CASE WHEN gn.mgr_type = '1' 
    THEN CONCAT(usr.user_firstname,' ',usr.user_lastname) WHERE usr.user_id=gn.mgr_id LIMIT 1
    ELSE gn.group_name WHERE gn.group_id=gn.mgr_id LIMIT 1
    END AS mgr_name
FROM group_names gn...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM