多表关联查询数据时报错:
Column ‘tag’ in where clause is ambiguous
sql1 = "select * from a inner join b on a.id = b.id where tag = '%s' order by rand() limit 1,5" %(tag)
原因:查询分析器分不清 tag 是哪个表的, 在 where 条件当中明确条件,例如如果 tag 是别名为 a 表的字段 写成
a.tag
即可。
sql1 = "select * from a inner join b on a.id = b.id where a.tag = '%s' order by rand() limit 1,5" %(tag)
补充:
select * from a inner join b on a.id = b.id:多表查询;
order by rand():查询结果随机
排序;
limit 1,x:查询结果取前x
个显示;
(完。)