Mysql SELECT查询中的IF和CASE语句及其子查询

Mysql SELECT查询中的IF和CASE语句及其子查询,mysql,subquery,inner-join,Mysql,Subquery,Inner Join,如果菜单项,下面的查询返回NULL。父项不是0,这是错误的 我在这里试图做的是,如果菜单项.parent行的值为0,则将其作为原始值返回,但如果值不是0,则返回相应行的名称(varchar),该行的值是什么 你知道我为什么一直是空的吗 注意:所有其他选定列都是正常的,因此查询本身是正常的 谢谢 示例1: SELECT ... ... ( CASE WHEN menu_items.parent = '0' THEN menu_items.parent ELSE (SE

如果
菜单项,下面的查询返回NULL。父项
不是0,这是错误的

我在这里试图做的是,如果
菜单项.parent
行的值为0,则将其作为原始值返回,但如果值不是0,则返回相应行的名称(varchar),该行的值是什么

你知道我为什么一直是空的吗

注意:所有其他选定列都是正常的,因此查询本身是正常的

谢谢

示例1:

SELECT
...
...
(
   CASE
      WHEN menu_items.parent = '0' THEN menu_items.parent
      ELSE (SELECT name FROM menu_items WHERE id = menu_items.parent)
   END
) AS ParentID
...
...
FROM menus
INNER JOIN menu_items ...
...
...
SELECT
...
...
IF
(
   menu_items.parent = '0',
      menu_items.parent,
      (SELECT name FROM menu_items WHERE id = menu_items.parent)
) ParentID
...
...
FROM menus
INNER JOIN menu_items ...
...
...
SELECT
menus.name AS MenuName,
menu_items.id AS MenuItemsId,
menu_items.name AS MenuItemsName

/*
Sub section should go here
*/


FROM users
INNER JOIN assigned_menus ON assigned_menus.fk_users_id = users.id
INNER JOIN menu_items ON menu_items.id = assigned_menus.fk_menu_items_id
INNER JOIN menus ON menus.id = menu_items.fk_menus_id
WHERE
users.id = ? AND
users.is_active = 'YES' AND
menu_items.is_active = 'YES' AND
menus.is_active = 'YES'
ORDER BY
MenuName,
MenuItemsName
示例2:

SELECT
...
...
(
   CASE
      WHEN menu_items.parent = '0' THEN menu_items.parent
      ELSE (SELECT name FROM menu_items WHERE id = menu_items.parent)
   END
) AS ParentID
...
...
FROM menus
INNER JOIN menu_items ...
...
...
SELECT
...
...
IF
(
   menu_items.parent = '0',
      menu_items.parent,
      (SELECT name FROM menu_items WHERE id = menu_items.parent)
) ParentID
...
...
FROM menus
INNER JOIN menu_items ...
...
...
SELECT
menus.name AS MenuName,
menu_items.id AS MenuItemsId,
menu_items.name AS MenuItemsName

/*
Sub section should go here
*/


FROM users
INNER JOIN assigned_menus ON assigned_menus.fk_users_id = users.id
INNER JOIN menu_items ON menu_items.id = assigned_menus.fk_menu_items_id
INNER JOIN menus ON menus.id = menu_items.fk_menus_id
WHERE
users.id = ? AND
users.is_active = 'YES' AND
menu_items.is_active = 'YES' AND
menus.is_active = 'YES'
ORDER BY
MenuName,
MenuItemsName
实际查询:

SELECT
...
...
(
   CASE
      WHEN menu_items.parent = '0' THEN menu_items.parent
      ELSE (SELECT name FROM menu_items WHERE id = menu_items.parent)
   END
) AS ParentID
...
...
FROM menus
INNER JOIN menu_items ...
...
...
SELECT
...
...
IF
(
   menu_items.parent = '0',
      menu_items.parent,
      (SELECT name FROM menu_items WHERE id = menu_items.parent)
) ParentID
...
...
FROM menus
INNER JOIN menu_items ...
...
...
SELECT
menus.name AS MenuName,
menu_items.id AS MenuItemsId,
menu_items.name AS MenuItemsName

/*
Sub section should go here
*/


FROM users
INNER JOIN assigned_menus ON assigned_menus.fk_users_id = users.id
INNER JOIN menu_items ON menu_items.id = assigned_menus.fk_menu_items_id
INNER JOIN menus ON menus.id = menu_items.fk_menus_id
WHERE
users.id = ? AND
users.is_active = 'YES' AND
menu_items.is_active = 'YES' AND
menus.is_active = 'YES'
ORDER BY
MenuName,
MenuItemsName
菜单项表

ID      name                    parent       fk_menu_items_id
1   Menus           0       1
2   Roles           0       1
3   Permissions     0       1
4   Users           0       1
5   Files           0       1
6   File Uploads        0       1
7   University      6       1
8   Details         6       1
9   Progress        6       1
10  Assg            6       1
11  Applications        0       2
12  New             11      2
13  Edit            11      2
14  Rejected        11      2
15  Approved        11      2
16  Exs         0       2
17  Assm            16      2
18  Stf         0       3
19  Std         0       3
20  Prg         19      3
21  Comm            0       4
22  Sms         21      4
23  Sms2            21      4
24  New2            0       4
25  Act         0       4

菜单项。如果
菜单项,则名称
不同。父项
不是
0
,因此您只返回同一行的名称,而不是编号:)非常感谢。