【当心!】多个like查询会出现大量重复数据
发布时间:2020-12-11 23:20:40 所属栏目:MySql教程 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 ===============================问题描述===================================两个表表usersid username1 andy2 jack3 leo4 kimi表carid title useri
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 ===============================问题描述=================================== 两个表 表users id username 1 andy 2 jack 3 leo 4 kimi 表car id title userid chnname 1 bmw750 1 宝马 2 bmw530 1 宝马 3 bmw 2 宝马 4 benzc2 1 奔驰 5 benzE3 2 奔驰 SQL:select * from a.*,b.* from car a inner join username b on a.userid = b.id 现在需要增加like查询条件 查询title类似于bmw的 select * from a.*,b.* from car a inner join username b on a.userid = b.id and a.title like '%bmw%' 这个语句会得到三条结果。正确的。但如果我再加上一个like条件就不行 查询title类似于bmw chnname类似于宝马 select * from a.*,b.* from car a inner join username b on a.userid = b.id and a.title like '%bmw%' or a.chnname like '%宝马%' 这样的话会得到一大堆的重复数据 ==============================分析================================= 之所以会得到那么多重复数据是因为上面的SQL相当于 select a.*,b.* from car a inner join username b on a.userid = b.id AND a.title like '%bmw%' UNION ALL select a.*,b.* from car a inner join username b on a.chnname like '%宝马%' ===============================解决================================= 在like条件中加()或者使用where 1.select * from a.*,b.* from car a inner join username b on a.userid = b.id and (a.title like '%bmw%' or a.chnname like '%宝马%') 2.select a.*,b.* from car a inner join username b on a.userid = b.id where a.title like '%bmw%' or a.chnname like '%宝马%' 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |