PostgreSQL - ¿2 instrucciones SELECT de 2 tablas diferentes en 2 columnas?

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%;


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 ( [0] => Array ( [lst_id] => 100007 ) [1] => Array ( [lst_id] => 100008 ))

This example is if 1 item from each table is matched

Array ( [0] => Array ( [prd_id] => 100006 ) [1] => 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

Isn't that a simple full outer join on title? -

Well, that is an example. It's part of a search query I'm working on and there can be many conditions after the WHERE including matching other fields in each table. I was just trying to use that as example of what I was trying to accomplish. -

Do your queries produce the same number of rows? -

not necessarily, whatever it finds in the tables it will return. There could be 10 row that match in one table and 2 the matched from the other. Just depends on if query finds anything. The problem is I need it to use the specific ID column for each table so I can use PHP to do stuff to it, so they can't be in the same column because it will then show the same key for all values that are returned. -

So if there are 10 rows returned from one table and 2 rows retruned from the other, do you want to see 12 rows or 10 rows returned? -

1 Respuestas

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%
SELECT 0 prd_id, lst_id FROM lists WHERE title LIKE %$var%;

respondido 09 mar '12, 16:03

Well, the problem that I get with that is it will show 0 for column it doesn't match. Is it possible for it just not to show up if there is not match. Your answer will give me this Array ( [0] => Array ( [prd_id] => 100452 [lst_id] => 0 ) [1] => Array ( [prd_id] => 0 [lst_id] => 100008 ) [2] => Array ( [prd_id] => 0 [lst_id] => 100007 ) ) instead can the query be done to give me this instead Array ( [0] => Array ( [prd_id] => 100452 ) [1] => Array ( [lst_id] => 100008 ) [2] => Array ( [lst_id] => 100007 ) ) - Panamá Jack

@Pjack: I'm not familiar with PHP, so I don't understand what you are asking. Can you format your desired results as query output, instead? - user359040

I did in the first post. Like this +-----------------+ | prd_id | lst_id | +-----------------+ | 100452 | 100008 | +-----------------+ - Panamá Jack

the comments don't really format well. But at the top in the org post, I have the table example with the word value in each row. That's how I want the output, but the values won't say value it will actually have the id # that it returns if a match is found. Otherwise the rows doesn't even show up. - Panamá Jack

@Pjack: The actual values are not included in your original question, while this comment only allows for 1 value from each table and appears to contradict your earlier comment where you said "@Mark, I want to see all 12 rows". I suggest you edit your original question to clearly display your desired query output, for a sample set of data. - user359040

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.