duanjizhan9353 2012-02-07 12:00
浏览 266
已采纳

有没有替代MYSQL HAVING

I'm trying to find some records based on the tags they have. In order to find out which tags a record has i add them to the result using a subquery. To find out which results should be returned i added a having statement on the end of the query. But something tells me this is not the best way.

SELECT e.id, e.title, e.text, e.introduction,
  UNIX_TIMESTAMP(e.starts_on) AS starts_on,
  UNIX_TIMESTAMP(e.ends_on) AS ends_on,
  m.id AS meta_id,
  m.url,
  cm.title AS category_title,
  cm.url AS category_url,
  CONCAT(
    ",",
    (
      SELECT GROUP_CONCAT(t.id)
      FROM modules_tags AS mt
      JOIN tags AS t ON t.id = mt.tag_id
      WHERE mt.other_id = e.id
    ),","
  ) AS tags_search
FROM event_posts AS e
INNER JOIN meta AS m ON e.meta_id = m.id
LEFT JOIN event_categories AS c ON e.category_id = c.id
LEFT JOIN meta AS cm ON c.meta_id = cm.id
LEFT JOIN modules_tags AS mt ON mt.other_id = e.id
LEFT JOIN tags AS t ON t.id = mt.tag_id
WHERE 1 HAVING tags_search LIKE '%,5,%' AND tags_search LIKE '%,6,%'
  • 写回答

1条回答 默认 最新

  • dongsheng1238 2012-02-07 12:30
    关注

    FIND_IN_SET() is the best way to find values in comma separated values in MySQL.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集