Link to home
Start Free TrialLog in
Avatar of Ruttensoft
Ruttensoft

asked on

SQL Query...

Hello

I have a question to the fastest way to read the solution out of my database.

I have several Tables:

Table1
ID                    Value
-----------------------------
1                     Test
2                     Hello
3                     What


Table 2
ID                    Value
-----------------------------
1002                  1
1030                  2
1053                  3


What I now have is ID 1030 from Table 2, and I want to get "Hello", because Table2.Value = Table1.ID

How can I get that with one query?

SELECT Table1.Value From Table1
WHERE Table1.ID = Table2.Value AND Table2.ID = 1030

?
Avatar of Thomasian
Thomasian
Flag of Philippines image

hi Ruttensoft,

You need to put both tables in the form clause.
--On the query you posted, just add Table2 on the FROM clause
SELECT Table1.Value From Table1, Table2
WHERE Table1.ID = Table2.Value AND Table2.ID = 1030
 
--Same result using JOIN (Recommended)
--Change INNER JOIN to LEFT JOIN if you want to return NULL if the ID does not exist in Table1
SELECT T1.Value
FROM Table2 T2 INNER JOIN
     Table1 T1 ON T2.Value=T1.ID
WHERE T2.Id=1030

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dojolava
dojolava

Hi,

try:


SELECT Table1.Value From Table1, Table2
WHERE Table1.ID = Table2.Value AND Table2.ID = 1030

or, maybe better in this case:

SELECT Table1.Value FROM Table1 JOIN Table2 ON Table1.ID = Table2.Value WHERE Table2.ID = 1030

more details on joins: http://en.wikipedia.org/wiki/Join_(SQL)

cheers,
mathis