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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Other SQL Server 2008 Topics
 select row from 2 table where row x ( in table a)
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

hthuong
Starting Member

Vietnam
1 Posts

Posted - 11/10/2012 :  19:51:10  Show Profile  Reply with Quote


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
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 11/11/2012 :  07:21:42  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.02 seconds. Powered By: Snitz Forums 2000