I'm having trouble figuring out this query. I'm using PHP and postgresql 9.1. I want to be able to select fields from rows based on a criteria in another field like so.
Table A
has columns:
vid, title, description, col4, col5, col6, col7
Table B
has columns:
pid, title, description, colx, coly
I want to search title
and description
from both tables and if a match is found, return the vid
or pid
of the row it was found in.
What I've tried so far:
SELECT vid FROM tableA WHERE title LIKE %somevalue%
UNION
SELECT pid FROM tableB WHERE title LIKE %somevalue%
Problem is when I do that and it finds a match for tableB
which returns the correct value but assigned to vid
column in the array.
The php array displays
Array ( [0] => Array ( [vid] => 100007 ) )
When it should be
Array ( [0] => Array ( [pid] => 100007 ) )
It doesn't give me the correct column name for the table. It only gives me the column name of the first select statement.
Also, it only returns one or the other. If the value is found in both I want both values with both columns.