mysql union查询出来有重复

2025年03月01日 06:54
有2个网友回答
网友(1):

你这样肯定重复,如果有的数据满足你的多个条件,就会导致重复,要想不重复,可以用下面的语句:
select *,case when `shop_name` like '%千叶%' then 'title' else case when `shop_address` like '%千叶%' then 'dz' else 'text' end end as type
from rice_shop_shoplist
where `shop_name` like '%千叶%'
or `shop_address` like '%千叶%'
or `shop_text` like '%千叶%'

无需联合,使用一个语句查询出你要的数据,效果应该更高,使用case when ... then ... else ... end这样的字句获得匹配的类型。

网友(2):

不用union用left join ,效果好,也不会重复