php - MySQL怎么样实现多个表的或查询?
大家讲道理
大家讲道理 2017-04-10 17:55:50
[PHP讨论组]


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

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

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回复(2)
PHPz

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}';

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

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号