Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Other SQL Server 2008 Topics
 select row from 2 table where row x ( in table a)

Author  Topic 

hthuong
Starting Member

1 Post

Posted - 2012-11-10 : 19:51:10


I have two table

Table login

account_id | name
========================
12345 | thuong1
12346 | thuong2
12347 | thuong3

Table char



account_id | char
========================
12345 | name1
12345 | name2
12345 | name3
12345 | name4


I want to select ...

SQL 1

SELECT account_id FORM char WHERE char='name1'

I will have result

12345

SQL 2

SELECT login.account_id, login.name, char.char WHERE login.account_id=char .account_id GROUP BY login.account_id , login.name, char.account_id, char.char HAVING login.account_id = 12345

Result

account_id | name | char
12345 | thuong1 | name1
12345 | thuong1 | name2
12345 | thuong1 | name3


How i select it with 1 one sql query?

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-11-11 : 07:21:42
Seems like there is something not quite right with the data or the setup - although I am not able to tell you what it is without understanding the business logic better. May be a query such as this is what you are looking for?
select
a.account_id,
a.name,
b.char
from
tableA a
inner join tableB b on a.account_Id = b.account_id
where
a.account_id in
(
select x.account_id from tableB x
where x.char = 'name1'
)
Go to Top of Page
   

- Advertisement -