0

我对sql有一个简单而愚蠢的疑问。我必须写下如下查询。在这里,id 不是唯一的。但是当我执行以下查询时,我会得到唯一值作为输出。我如何获得多个值?

select name from students_table where id in (select id from students_id);

Ex : Lets say students_table has name   id
                                 -----  ---
                                 john    1
                                 jack    2

     And students_id table has   id    
                                 ---
                                  1
                                  1
                                  1
                                  2
                                  ...

If i write the query in the above way i get name
                                            ----
                                            john
                                            jack

Instead i want my output as name
                            ----
                            john
                            john
                            john
                            jack
4

3 回答 3

2
select st.name from students_id si
join students_table st on st.id = si.id;
于 2012-09-06T22:34:37.540 回答
1

你需要JOIN两个表。试试这个,

SELECT  a.Name
FROM    student_table a
        INNER JOIN student_ID b
             on a.ID = b.ID

这就是你要找的。

于 2012-09-06T22:33:07.120 回答
0

你想做一个加入:

select t1.name from students_table t1 inner join students_id t2 on t1.id = t2.id
于 2012-09-06T22:34:54.233 回答