Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to join result from two tables with same field into one field?

I have tables like this:

   Table1                Table2
   name1 | link_id      name2  |  link_id
   text       1         text         2
   text       2         text         4

And I wanna have result:

name1     name2     link_id
text      text         1
text      text         2
text      text         4

How I can do this?

ADD: Sry, my English in not good. I have device, device_model and device_type tables with duplicate field counter_set_id. I wanna select fields from counter_set with all values of counter_set_id. I need to fetch values only from counter_set_id fields

Now I have this query:

SELECT  `dev`.`counter_set_id`, `mod`.`counter_set_id`, `type`.`counter_set_id`
FROM    `device` AS `dev`
LEFT JOIN   `device_model` AS `mod` ON `dev`.`device_model_id` = `mod`.`id`
LEFT JOIN   `device_type` AS `type` ON `mod`.`device_type_id` = `type`.`id`
WHERE   `dev`.`id` = 4;

This returns 3 columns but I need all values in one column

This is final variant I think:

SELECT  `dev`.`counter_set_id`
FROM        `device` AS `dev` LEFT OUTER JOIN
        `device_model` AS `mod` ON `dev`.`device_model_id` = `mod`.`id`
WHERE   `dev`.`id` = 4 AND
        `dev`.`counter_set_id` IS NOT NULL
UNION
SELECT  `mod`.`counter_set_id`
FROM        `device_model` AS `mod` LEFT OUTER JOIN
        `device` AS `dev` ON `mod`.`id` = `dev`.`device_model_id`
WHERE   `mod`.`counter_set_id` IS NOT NULL;
like image 210
VeroLom Avatar asked Nov 29 '10 14:11

VeroLom


People also ask

How do you join two tables in SQL that has no common fields?

Using the “FROM Table1, Table2” Syntax One way to join two tables without a common column is to use an obsolete syntax for joining tables. With this syntax, we simply list the tables that we want to join in the FROM clause then use a WHERE clause to add joining conditions if necessary.

Which join returns values when there is a match between two tables?

The JOIN or INNER JOIN does not return any non-matching rows at all. It returns only the rows that match in both of the tables you join. If you want to get any unmatched rows, you shouldn't use it. The LEFT JOIN and the RIGHT JOIN get you both matched and unmatched rows.

How do I combine two data tables in SQL?

SQL JOIN. A JOIN clause is used to combine rows from two or more tables, based on a related column between them. Notice that the "CustomerID" column in the "Orders" table refers to the "CustomerID" in the "Customers" table. The relationship between the two tables above is the "CustomerID" column.


3 Answers

Based on the sample tables and desired output you provided, it sounds like you might want a FULL OUTER JOIN. Not all vendors implement this, but you can simulate it with a LEFT OUTER join and a UNION to an EXCEPTION join with the tables reversed like this:

Select name1, name2, A.link_id
From table1 A Left Outer Join
     table2 B on A.link_id = B.link_id
Union
Select name1, name2, link_id
From table2 C Exception Join
     table1 D on C.link_id = D.link_id

then your output would be like this:

NAME1   NAME2    LINK_ID
=====   =====    =======
text    <NULL>         1
text    text           2
<NULL>  text           4
like image 117
Lynette Duffy Avatar answered Oct 05 '22 03:10

Lynette Duffy


At first i thought a join would work but now i'm not sure... Im thinking a union of some type.

in all honesty this is a bad design imo.

select * from table1
union
select * from table2
like image 27
Patrick Avatar answered Oct 05 '22 03:10

Patrick


I'm assuming that you're joining on the link_id field. A normal join would result in four rows being returned (two of which are identical). What you're really asking for isn't combining fields, but getting only the distinct rows.

Just use the distinct keyword:

select distinct t1.name1, t2.name2, t1.link_id
from Table1 as t1
inner join Table2 as t2
    on t1.link_id = t2.link_id
like image 21
Justin Niessner Avatar answered Oct 05 '22 03:10

Justin Niessner