I have two tables: articles and businesses. They do not share any columns that I can join them on. However, I want to search both of them at the same time from a single text input form. Is this possible with a single query?
Thanks Much.
Printable View
I have two tables: articles and businesses. They do not share any columns that I can join them on. However, I want to search both of them at the same time from a single text input form. Is this possible with a single query?
Thanks Much.
yes, sort of
you have to select the same number of compatible columns from each table
Code:select 'article ' as source
, aid as pkey
, title as descr
from articles
where title like '%searchterm%'
union all
select 'business'
, bus_no
, bus_name
from businesses
where bus_name like '%searchterm%'
Thank you very much for the reply. I'm new to the union syntax. I tried using:
but I'm getting an error near "Union All". Could you explain what is supposed be in the single quote after the SELECT?Code:SELECT 'article_body ' AS source, article_id AS pkey, article_title AS descr
FROM articles
WHERE article_title
LIKE '%searchterm%'
UNION ALL
SELECT 'name', bus_id
FROM businesses
WHERE name
LIKE '%searchterm%'
LIMIT 0 , 30
Thank you.
what database? no, wait, let me guess, mysql
what version? for UNIONs, you need to be on at least 4.0 (the release prior to the current release)
also, you need to have the same number of columns in each of the SELECTs