0

i've a situation like the following :

  • i've a long query that join a table
  • all the columns in joined tabled contain a column 'name'. in this table there are two names 'A' and 'B' (but can be C,D......Z, i don't know how many names we can have) and both of them have multiple rows, so i've n rows with name A and n rows with name B
  • sometimes, based on the user input, i need to join the entire table (with all the names) BUT only put some condition where the name is 'A' (for example).

so :

joint table myTable (it will take all the results A,B,C...) but if the name = 'A' then A.priority = A.userInput (for B,C... get them without additional conditions)

so, is there a solutions for this or i need to do multiple calls to the database ?

Thanks.

dack funk
  • 43
  • 6
  • Ok, if I understood this correctly, you're looking to conditionally join a table if one of the parameters has a certain value (`'A'`)? – El_Vanja Nov 20 '20 at 14:57
  • 1
    Please [edit] your question to give us a [mcve]. Please include a few sample rows from each table and sample results. – O. Jones Nov 20 '20 at 14:59
  • @El_Vanja need to get all the entire joined table but i need to apply conditions only to the rows ,in the joined table, that contain name 'A', and then get the others (B, C, ...) without extra condition conditions – dack funk Nov 20 '20 at 15:02
  • Do you mean `WHERE name != 'A' OR A.priority = A.userInput`? – Michal Hynčica Nov 20 '20 at 16:38

1 Answers1

1

I would use UNION ALL clause for this

SELECT *
  FROM TABLE1 T1
  JOIN TABLE2 T2
    ON T1.somecolumn = T2.somecolumn
  WHERE T2.name <> 'A'
UNION ALL
SELECT *
  FROM TABLE1 T1
  JOIN TABLE2 T2
    ON T1.somecolumn = T2.somecolumn
  WHERE T2.name = 'A' AND some condition
Felippe Duarte
  • 14,901
  • 2
  • 25
  • 29