Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IF and CASE statements in SELECT query with a subquery in them

Queries below return NULL if menu_items.parent is not 0 which is wrong.

What I'm trying to do here is, if value of menu_items.parent row is 0 then return it as original value but if the value is not 0 then return the name (varchar) of the corresponding row of what ever the value is.

Any idea why I keep getting NULL?

Note: All the other selected columns comes as normal so the query itself is fine.

Thanks

EXAMPLE 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 ...
...
...

EXAMPLE 2:

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 ...
...
...

ACTUAL QUERY:

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

menu_items TABLE

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
like image 310
BentCoder Avatar asked Mar 07 '13 12:03

BentCoder


People also ask

Can you use a Case statement in a subquery?

Subquery ExampleThe following query example uses the Subquery inside a Case Statement in SQL Server. First, the Subquery will execute and finds the Average of the Sales amount. Next, it will check whether the Sales are greater than the Average Sales (1970.9055).

Can you use subquery in a WHERE clause of the SQL query?

A subquery can be nested inside the WHERE or HAVING clause of an outer SELECT , INSERT , UPDATE , or DELETE statement, or inside another subquery.

Is it better to use CTE or subquery?

Advantage of Using CTE CTE can be more readable: Another advantage of CTE is CTE are more readable than Subqueries. Since CTE can be reusable, you can write less code using CTE than using subquery. Also, people tend to follow the logic and ideas easier in sequence than in a nested fashion.


1 Answers

 SELECT
...
...
(
   CASE
      WHEN menu_items.parent = '0' THEN menu_items.parent
      ELSE (SELECT mi.name FROM menu_items mi WHERE mi.id = menu_items.parent)
   END
) AS ParentID
...
...
FROM menus
INNER JOIN menu_items ...
...
...
like image 55
Romil Kumar Jain Avatar answered Sep 28 '22 17:09

Romil Kumar Jain