I asked a similar question yesterday but still having a slight problem on output. Unions only always combines it into one column and there is no relation to do a join. Here is what I want. I don't want to combine.
SELECT prd_id FROM products WHERE title LIKE %$var%; AND SELECT lst_id FROM lists WHERE title LIKE %$var%;
I want both of these in one query and output the results it finds into 2 columns IF there is a result for either one. I also would like to use a LIMIT at the end too.
So for example if both tables have an item that matches the title then it will return something like. Note the values will be whatever it matched in each table. The point is just to be in 2 columns instead of one like Unions output.
+-----------------+ | prd_id | lst_id | +-----------------+ | value | value | +-----------------+
I get close but just can't it exactly.
To make it clear, the PHP print_r output should be like this. This example is if 2 items are matched in the list table only
Array (  => Array ( [lst_id] => 100007 )  => Array ( [lst_id] => 100008 ))
This example is if 1 item from each table is matched
Array (  => Array ( [prd_id] => 100006 )  => Array ( [lst_id] => 100008 ))
There can be any number of matches from either of the tables.
preguntado el 09 de marzo de 12 a las 16:03
The simplest answer would be to union the results into different column, like so:
SELECT prd_id, 0 lst_id FROM products WHERE title LIKE %$var% UNION ALL SELECT 0 prd_id, lst_id FROM lists WHERE title LIKE %$var%;