tags:

views:

42

answers:

4

I have 4 tables each with different columns but they all have one column in common. This is an integer identifier column. So I will have some integer x, and I want all the rows from all 4 tables that have this one id column equal to x.

Ive tried something similar to:

SELECT table1.col1, table2.col2 FROM table1 LEFT JOIN table2 ON table1.id=x OR coastlinessports.id=x

and I get back rows which have both the columns from both tables in the same row.

So one result block would have:

table1.col1, table2.col2

But I really want:

table1.col1
tale2.col2

Is there a way I can do this without doing 4 select queries in a row?

A: 

This probably won't answer your question, but there's something weird about the JOIN.

Usually the "ON" condition refers to both tables being joined, similar to this:

... FROM table1 LEFT JOIN table2 ON table1.id = table2.id ...

I guess there can be cases where you wouldn't do that, but I can't think of any.

mjomble
Better as a comment :-) Welcome to SO.
pst
A: 

If the table is called the same you can use USING And for the part of the given value, use WHERE

select * from table1 join table2 using(commonColumn) join table3 using(commonColumn) join table4 using(commonColumn) where commonColumn="desiredValue"

Update: on a second read of your question You want this?

All rows of table1 where commonColumn="desiredValue"
Followed by
All rows of table2 where commonColumn="desiredValue"
Followed by
All rows of table3 where commonColumn="desiredValue"
Followed by
All rows of table4 where commonColumn="desiredValue"

If that's so, you need to use a UNION (and you have to make 4 selects) IF the number of columns differs, you need to fill the gaps whit aliases

SELECT col1, col2, col3, col4 from table1 where commonColumn="desiredValue"  
UNION
SELECT col1, col2, 0 as col3, 0 as col4 from table2 where commonColumn="desiredValue"  
...
The Disintegrator
A: 

You should check out this post. It seems like what you are asking for: http://ask.sqlteam.com/questions/870/pivoting-multiple-rows-into-one-row-with-multiple-columns

spinon
+1  A: 

If you want sequential rows from different tables, and for each table to return a different number of rows, then you can use UNION. However, UNION requires each SELECT to return the same number of columns, so you will need to fill in the missing columns with a value (or NULL), like this:

DROP TABLE IF EXISTS `table1`;
DROP TABLE IF EXISTS `table2`;

CREATE TABLE `table1` (
  `id` int(11) NOT NULL auto_increment,
  `col1` VARCHAR(255),
  `col2` VARCHAR(255),
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB;

CREATE TABLE `table2` (
  `id` int(11) NOT NULL auto_increment,
  `col1` VARCHAR(255),
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB;

INSERT INTO `table1` VALUES
    (1, '1,1', '1,2'),
    (2, '2,1', '2,2');

INSERT INTO `table2` VALUES
    (1, '1,1'),
    (2, '2,1');

SELECT `id`, `col1`, `col2` FROM `table1` WHERE `id` = 1
UNION
SELECT `id`, `col1`, NULL AS `col2` FROM `table2` WHERE `id` = 1;

+----+------+------+
| id | col1 | col2 |
+----+------+------+
|  1 | 1,1  | 1,2  |
|  1 | 1,1  | NULL |
+----+------+------+

If you want to further process the UNION result set, you can wrap it in another SELECT, like this:

SELECT `col1`, `col2` FROM (
    SELECT `id`, `col1`, `col2` FROM `table1` WHERE `id` = 1
    UNION
    SELECT `id`, `col1`, NULL AS `col2` FROM `table2` WHERE `id` = 1
) AS `t1`
ORDER BY col2;

+------+------+
| col1 | col2 |
+------+------+
| 1,1  | NULL |
| 1,1  | 1,2  |
+------+------+

Is that what you are after?

Mike