香港rap歌曲排行榜:SQL中两表其中一字段相同,怎样显示指定的相同字段?

来源:百度文库 编辑:高考问答 时间:2024/04/27 21:33:25
两张表中都有相同的字段ID。
Select * from Table1,Table2 where Table1.UserName=Table2.Name
执行完后,用什么语句显示Table2中的ID?
在ACCESS中可以用<%=rs("Table1.ID")%>,可SQL中应该怎么写?

Select table1.id as id1,table2.id as id2 ,table1.username,table2.name from Table1 inner join Table2 on Table1.UserName=Table2.Name

查询的时候不用*,明确指出选择哪一个表的id,有重复就取别名

在读取的时候<%=rs("ID1")%>,<%=rs("ID2")%>

另,你原来的sql语句可能不太正确

select b.name as name,其它字段名...... from table1 a,table2 b where a.username=b.name
注:如果你在查询中用 * 号,相同的字段名就不能以同字段名出现,只有将其中一个字段名赋加别名,如:username as '123' 这样在显示时其列名就是以123出现。

Select a.ID,b.ID from Table1 as a,Table2 as b where a.UserName=b.Name