FeliceM
FeliceM

Reputation: 4199

SQL join two queries

I am a bit lost here. I would like to join these two queries into one to avoid two connections and simplify the code.

 "SELECT uname FROM Projects WHERE id IN (SELECT MAX(id) FROM Projects)"

 "SELECT name FROM Users WHERE username like"+"'"+ uname +"'"

Right now I am using two queries and taking the result of the first into the second one. I am sure the two queries can become one but I do not know how to do it.

Upvotes: 1

Views: 177

Answers (3)

Rahul Tripathi
Rahul Tripathi

Reputation: 172378

You may try like this using the INNER JOIN considering that both of your tables are linked through User_ID

SELECT  u.name
FROM    Users u INNER JOIN Projects p ON u.username = p.uname
WHERE   p.ID = (SELECT MAX(id) FROM Projects)

Upvotes: 1

farzad
farzad

Reputation: 409

i think the following query will work:

SELECT name FROM Users WHERE username in 
    (SELECT uname FROM Projects WHERE id IN 
        (SELECT      MAX(id) FROM Projects))

Upvotes: 1

John Woo
John Woo

Reputation: 263693

You can simply use = rather than LIKE since I you are not using pattern symbol such as %.

SELECT  a.name
FROM    Users a
        INNER JOIN Projects b
            ON a.username = b.uname
WHERE   b.ID = (SELECT MAX(id) FROM Projects)

Upvotes: 1

Related Questions