• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: if conditions inside where sql

Resolved: if conditions inside where sql

0
By Isaac Tonny on 17/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I’m trying this logic not sure what am missing in this case

Answer:

You could use a case when aproach

CREATE tABLE table2(column1 int, column2 varchar(10))


INSERT INTO table2 VALUEs(1,’value1′),(2,’value1′),(13,’value2′),(44,’value2′)


CREATE tABLE table1(column1 int)


INSERT INTO table1 VALUES (1),(2),(13),(44)


select * from table1 t1 join table2 t2 on t1.column1=t2.column1 where CASE WHEN t1.column1 between 1 and 10 AND t2.column2 like ‘value1’ THEN TRUE WHEN t1.column1 between 11 and 20 AND t2.column2 like ‘value2’ THEN TRUE ELSE FALSE END


column1 | column1 | column2 ——: | ——: | :—— 1 | 1 | value1 2 | 2 | value1 13 | 13 | value2


db<>fiddle here

If you have better answer, please add a comment about this, thank you!

postgresql sql
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: navigate and redirect not redirecting to path in react router

24/03/2023

Resolved: Time Complexity: Find the number of people who know a secret leetcode 2327

24/03/2023

Resolved: Best way to create 3d matrix of variables in PULP

24/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.