首页 > MySQL怎么样实现多个表的或查询?

MySQL怎么样实现多个表的或查询?


我想要像上图那样在两个表里面查询username,只有其中一个表有这个数据就返回true,但是MySQL判断必须两个表都有才返回true。

请问有什么办法可以实现这样的查询呢?(两个表中任何一个表有数据则返回true)


select * from user_agent, user_cleck where user_agent.username = 'huibao' or user_clerk.username = 'huibao'这样可以吗?


select * from table1, table2 是多表联合查询,题主的情况只是两个单表查询的结果取合集,应该用 UNION

1、结果取合集

select * from user_agent where username='{$username}' 
union all 
select * from user_clerk where username='{$username}';

2、结果取合集,并去掉重复项

select * from user_agent where username='{$username}' 
union 
select * from user_clerk where username='{$username}';

题主的情况,两种都可以。

【热门文章】
【热门文章】