从2表中选择行,其中行x(在表a中)等于某个值

时间:2012-11-10 15:34:21

标签: sql

我有两张桌子

login

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

char

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

我想选择......

SQL 1

SELECT account_id FORM char WHERE char='name1'

我会得到结果

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

结果

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

我如何用一个SQL查询选择它?

1 个答案:

答案 0 :(得分:3)

SELECT  a.account_ID,
        a.name, 
        b.char
FROM    login a
        INNER JOIN char b
            ON a.account_ID = b.account_ID
WHERE   a.name='thuong1'