views:

29

answers:

2

HI, i have two tables 1- name, id, code 2- id, value, concept

One name can have two concepts and two values. i want to retreive this: Id, name, value1, value2.

How can i do that?

Tanks

A: 

You could do for example

SELECT t.id, t.name, t.value as value1, t2.value as value2
FROM table2 as t
     INNER JOIN table2 as t2 ON t.id = t2.id AND t.value <> t2.value

but there are many other ways available.

EDIT

SELECT t1.id, t1.name, t2a.value as value1, t2b.value as value2
FROM table1 as t 
     INNER JOIN table2 as t2a ON t.id = t2a.id
     INNER JOIN table2 as t2b ON t.id = tba.id AND t2b.value <> t2a.value

Note: solution assumes that there are 2 rows per id (in case of 3 rows you will get multiple rows per id in the results)

Unreason
looks good except you are only using the second table he mentioned and the 'name' column is in the first table. The only thing I would add is do another join with table1 as t1 on table1.id = table2.id so he would have the name field as t1.name
Kyra
yup, you're right, i've missread... updating
Unreason
+1  A: 

This will be a start:

SELECT
    T.id,
    T.name,
    V1.value AS value1,
    V2.value AS value2
FROM
    My_Table T
LEFT OUTER JOIN My_Values V1 ON V1.id = T.id
LEFT OUTER JOIN My_Values V2 ON V1.id = T.id

But the question here is, what differentiates value 1 from value 2? Is it a value in "concept", is it a matter of which was added to the DB first? Which has a higher id column for some other column? Your requirements are not fully spelled out.

Tom H.