1

I have a create that have sub query. To show as example, here is a query that has similarity with my own. Personally I have more table and more arguments on table (The subquery have left join for example). I simplified it for the question.

SELECT
   Content,
   CASE 
      WHEN S.User IN (SELECT UserId FROM Users WHERE Deleted = false) AND S.Permission Like '%ADMIN%' THEN 'Admin'
      ELSE 'Basic'
   END AS UserType,
   CASE 
      WHEN S.User IN (SELECT UserId FROM Users WHERE Deleted = false) THEN 'Here'
      ELSE 'Not'
   END AS Exist
FROM (
   SELECT *
   FROM MyTable
   WHERE Deleted = false
) as S;

I want to put the SELECT UserId FROM Users WHERE Deleted = false query into the main table as I'm using the result multiple times. In the example, I'm using the content 2 times. It's only for optimization. Hope it can really help performances.

I tried something like that:

SELECT
   Content,
   CASE 
      WHEN S.User IN (AvailableUsers) THEN 'Here'
      ELSE 'Not'
   END AS Exist,
   CASE 
      WHEN S.User IN (AvailableUsers) AND S.Permission Like '%ADMIN%' THEN 'Admin'
      ELSE 'Basic'
   END AS UserType
FROM (
   SELECT *, (SELECT UserId FROM Users WHERE Deleted = false) as AvailableUsers
   FROM MyTable
   WHERE Deleted = false
) as S;

But I get Subquery returns more than 1 row. How can I make as it works and run the query only one time?

PS: I don't have actual data as even me, while checking the query, wasn't looking for the data exactly.

5
  • Please add some data examples and expected result in text format. I think your queries can be written easily using LEFT JOIN Commented Apr 5, 2023 at 13:54
  • @ErgestBasha I understand why you want that, but a left join will not fit of what I want as it will be on the subquery and not the main one. For input/output data, I will try to give you one, but as it's sample things, my data is clearly different of that
    – Elikill58
    Commented Apr 5, 2023 at 14:09
  • Does MyTable have a unique column ? You need something unique to force the subquery to return only one row . Something like SELECT Content, (SELECT UserId FROM Users WHERE Deleted = false AND MyTable.uniquecolumn=Users.something) Commented Apr 5, 2023 at 14:47
  • That's the fact: both have unique column, but not in common, and I don't want to link them as the full AvailableUsers will be used in switch/case after it. Maybe the optimisation I'm trying to do isn't necessary, I'll more explain in the question
    – Elikill58
    Commented Apr 5, 2023 at 14:59
  • Try to avoid subqueries. I don't understand your schema or what you are attempting to do, but I think it could be achieved with a FROM users JOIN mytable ON ....
    – Rick James
    Commented Apr 7, 2023 at 22:51

1 Answer 1

0

I found a way. I'm make the check on the query of MyTable, store as variable and use it. I'm doing like that:

SELECT
   Content,
   CASE 
      WHEN S.UserAvailable = 1 THEN 'Here'
      ELSE 'Not'
   END AS Exist,
   CASE 
      WHEN S.UserAvailable = 1 AND S.Permission Like '%ADMIN%' THEN 'Admin'
      ELSE 'Basic'
   END AS UserType
FROM (
   SELECT *,
      CASE
         WHEN User IN (SELECT UserId FROM Users WHERE Deleted = false) THEN 1
         ELSE 0
      END AS UserAvailable
   FROM MyTable
   WHERE Deleted = false
) as S;

Then it works, and I'm making the request only one time

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.