在搜索过滤器查询中找不到所需的结果

时间:2019-03-22 09:56:03

标签: mysql sql database relational-database

我正在尝试从最近2天开始,以在查询中获得所需的结果,但是无法做到这一点。基本上,我正在查询餐厅网站“基于标签”的搜索过滤器,以搜索喜欢的菜肴。

有一个单独的标签表,其中相对于meal_id,restaurant_id保存了很多标签。餐馆->标签和餐->标签的多对多关系。

用户输入的标签越多,结果就越少。

请查看查询

SELECT
   ep_tag.tag_en as tag_meal,
   ep_tag2.tag_en as tag_restaurant,
   `ep_meal`.id as id_meal,
   `ep_meal`.name as name_plat,
   `ep_meal`.`id_budget`,
   `ep_meal_favorite`.date,
   `ep_restaurant`.id as `id_restaurant`,
   `ep_restaurant`.* 
from
   `ep_meal` 
   inner join
      `ep_meal_favorite` 
      on `ep_meal_favorite`.`id_meal` = `ep_meal`.id 
   inner join
      `ep_restaurant` 
      on `ep_meal_favorite`.`id_restaurant` = `ep_restaurant`.`id` 
   inner JOIN
      ep_tag 
      ON ep_meal_favorite.id_meal = ep_tag.id_meal 
   inner JOIN
      ep_tag as ep_tag2 
      ON ep_meal_favorite.id_restaurant = ep_tag2.id_restaurant 
where
   ep_meal_favorite.id_epicurean = 82 
   and `ep_restaurant`.id = 14 
   and `ep_meal`.id = 92 
   AND 
   (
      ep_tag.tag_en LIKE '%tuna%' 
      OR ep_tag2.tag_en LIKE'%tuna%'
   )
   AND 
   (
      ep_tag.tag_en LIKE '%karaoke%' 
      OR ep_tag2.tag_en LIKE'%karaoke%'
   )

enter image description here

上面发布的图片是当前结果,问题是查询返回记录,其中tag_meal是“金枪鱼” ,而tag_restaurant是“ karaoke” ,但是当匹配时返回空但是其他人没有。

我希望即使条件匹配的查询也能返回结果,假设我为“餐” 写了2个标签,为“餐厅” 没有写标签,反之亦然,同时,我希望查询使用AND,因为我想减少结果的数量。

在这方面请帮助我。谢谢。

1 个答案:

答案 0 :(得分:0)

正如我评论的那样,获取与一条或另一条记录匹配的所有行的方法是对它们进行“或”运算。

SELECT
   ep_tag.tag_en as tag_meal,
   ep_tag2.tag_en as tag_restaurant,
   `ep_meal`.id as id_meal,
   `ep_meal`.name as name_plat,
   `ep_meal`.`id_budget`,
   `ep_meal_favorite`.date,
   `ep_restaurant`.id as `id_restaurant`,
   `ep_restaurant`.* 
from
   `ep_meal` 
   inner join
      `ep_meal_favorite` 
      on `ep_meal_favorite`.`id_meal` = `ep_meal`.id 
   inner join
      `ep_restaurant` 
      on `ep_meal_favorite`.`id_restaurant` = `ep_restaurant`.`id` 
   inner JOIN
      ep_tag 
      ON ep_meal_favorite.id_meal = ep_tag.id_meal 
   inner JOIN
      ep_tag as ep_tag2 
      ON ep_meal_favorite.id_restaurant = ep_tag2.id_restaurant 
where
   ep_meal_favorite.id_epicurean = 82 
   and `ep_restaurant`.id = 14 
   and `ep_meal`.id = 92 
   AND 
   (
      ep_tag.tag_en LIKE '%tuna%' 
      OR ep_tag2.tag_en LIKE'%tuna%'
      OR ep_tag.tag_en LIKE '%karaoke%' 
      OR ep_tag2.tag_en LIKE'%karaoke%'
   )

这不是那么有效。您可以构建正则表达式进行优化。