mysql多表查询

来源:百度知道 编辑:UC知道 时间:2024/07/08 20:15:46
条件:
有2个表,分别名为a和b,a表中有id,pass字段,b表中有id,qq字段
要求:
查询b表中QQ=12345的a表中pass的值,两表id一样!

select b.qq,a.pass from a inner join b on a.id=b.id where b.qq='12345'



select b.qq,a.pass from a,b where a.id=b.id and b.qq='12345'

以上,希望对你有所帮助!

如果a表和b表的id字段是相关联的,那么就可以查询,sql语句如下:

select pass from a where id=(select id from b where qq=12345);

这是标准sql语句,不管在那种数据库产品都适用。不过有一点需要说明,如果子查询(select id from b where qq=12345)只返回一行值,可以用=;如果返回多行,可以使用in;如是子查询返回的id数不多,最好建议使用 in 来代替 =