My problem is that the website I'm currently working on has two tables for products and there's a search function that only searches one of the tables. I'd like to extend the search page of the website to cover both tables.
Effectively, I need two queries, which join their result set so I can use PHP to calculate relevancy to the user's search query.
mysql_query("SELECT * FROM product WHERE title LIKE '%$keyword%' OR content LIKE '%$keyword%'")
title
is a field that is on the product_spray
table as well, as is content
. How can I modify the above query to include results from the product
table and the product_spray
table?
I'm not sure it's a JOIN that I need, because there's no link between the two tables, they're exclusive to each other. Though, I'm not very experienced with MySQL, so if there's something I'm missing, let me know.
I've tried this:
mysql_query("SELECT * FROM product, product_spray WHERE product.title LIKE '%$keyword%' OR product.content LIKE '%$keyword%' OR product_spray.title LIKE '%$keyword%' OR product_spray.content LIKE '%$keyword%'")
The result set doesn't come out correctly, it seems to combine the results.
Essentially, what I need is to have a single query, but using two exclusive SELECT statements. Can this be done?
I'd even be open to doing something in PHP that will allow combining the result set of two queries.
I guess you want to join the rows (not the columns right?). And to solve your problem, you will use UNION
for this
SELECT columnA
FROM product
WHERE title LIKE '%$keyword%' OR
content LIKE '%$keyword%'
UNION
SELECT columnA
FROM product_spray
WHERE title LIKE '%$keyword%' OR
content LIKE '%$keyword%'
just make sure that both queries have the same column in the result set.
mysq_query("SELECT A.*,B.* FROM
product as A
left join product_spray as B ON B.title LIKE '%$keyword%' = A.title LIKE '%$keyword%'
WHERE B.content LIKE '%$keyword%' OR B.content LIKE '%$keyword%'")
AND don't A.* this sol - select A.title , A.content from xxx