I'm trying to aggregate data in a concise way from multiple tables. I'm not very experienced with Postgres or SQL, however, so I was hoping I could get some help from here!
The situation is as follows: I have three columns containing text, from two different tables. Let's call them table A and B. I want to search A.text1, A.text2 and B.text3 for the wildcard '%postgres%'. I want the result to return all the instances containing that wildcard to me.
What I would like is a result that looks something like this:
Type | Text |
---|---|
text1 | ... postgres ... |
text2 | ... postgres ... |
text3 | ... postgres ... |
My question is: is this feasible, and if it is feasible, can you point me in the right direction to achieve such a result?
The current solution I have created for this is:
select a.text1, a.text2, b.text3
from a
inner join b
on a.id = b.aId
where a.text1 like '%postgres%'
or a.text2 like '%postgres%'
or b.text3 like '%postgres%'
This works, but the result is kind of ugly. There are a lot of cells that contain '[null]' and I don't really like that. It looks like this:
a.text1 | a.text2 | b.text3 |
---|---|---|
null | ... postgres ... | null |
... postgres ... | ... postgres ... | null |
null | null | ... postgres ... |
Update: I have solved the problem! The sql I ended up with is:
select * from
(select 'text1' as type, text1 as text from a
union all
select 'text2' as type, text2 as text from a
union all
select 'text3' as type, text3 as text from b) as result
where result.text like '%postgres%'