doucheng5209 2016-08-13 13:20
浏览 273
已采纳

MySQL根据多个表条件选择

I am working on a quiz website and need to suggest a quiz the user may like based on the quiz they just took. Basically, if the quiz they just took has a tag I need to pull another quiz with the same tag. Otherwise, pull another quiz from the same category.

What I have is working 90% the way I want it to, but it seems very bulky to me. The 10% that's not working is I forgot to add a condition where it only pulls quizzes that are active. Something like WHERE q.active = 1 but no matter where or how I add that condition it does not work as planned. I either get an empty set or the same result I would was receiving before the condition was added.

EDIT:

To clarify, after reading the comment by @RiggsFolly...

If I add AND q.active = 1 to my first WHERE clause and there is only one other quiz with the same tag and it is not active I receive an empty set instead of receiving a result from the category select statement. Adding AND q.active = 1 to just the category select statement and not the tag select statement returns the proper result if there are no quizzes with the same tag.

SELECT 
    IFNULL(q.meta_title, q.title) AS title, 
    IFNULL(url, title) AS url, 
    1 istag
FROM tag_index t
LEFT JOIN tag_index ti 
    ON ti.tag_id = t.tag_id
LEFT JOIN quizzes q 
    ON q.id = ti.quiz_id
WHERE t.quiz_id = :quiz_id 
    AND ti.quiz_id != t.quiz_id

UNION ALL

SELECT 
    IFNULL(q.meta_title, q.title) AS title, 
    IFNULL(url, title) AS url, 
    0 istag
FROM category_index c
LEFT JOIN category_index ci 
    ON ci.category_id = c.category_id
LEFT JOIN quizzes q 
    ON q.id = ci.quiz_id
WHERE c.quiz_id = :quiz_id 
    AND ci.quiz_id != c.quiz_id 
    AND NOT EXISTS
        (SELECT 1 FROM tag_index WHERE quiz_id = :quiz_id)
ORDER BY RAND() LIMIT 1

Any help or suggestions would be greatly appreciated!

  • 写回答

2条回答 默认 最新

  • donglu1472 2016-08-13 15:20
    关注

    You also need to add the active = 1 condition into your subselect:

    SELECT 
        IFNULL(q.meta_title, q.title) AS title, 
        IFNULL(url, title) AS url, 
        1 istag
    FROM tag_index t
    LEFT JOIN tag_index ti 
        ON ti.tag_id = t.tag_id
    LEFT JOIN quizzes q 
        ON q.id = ti.quiz_id
    WHERE t.quiz_id = :quiz_id 
        AND ti.quiz_id != t.quiz_id
        AND q.active = 1
    
    UNION ALL
    
    SELECT 
        IFNULL(q.meta_title, q.title) AS title, 
        IFNULL(url, title) AS url, 
        0 istag
    FROM category_index c
    LEFT JOIN category_index ci 
        ON ci.category_id = c.category_id
    LEFT JOIN quizzes q 
        ON q.id = ci.quiz_id
    WHERE c.quiz_id = :quiz_id 
        AND ci.quiz_id != c.quiz_id 
        AND q.active = 1
        AND NOT EXISTS
            (SELECT 1
             FROM tag_index t
             JOIN tag_index ON ti.tag_id = t.tag_id
             JOIN quizzes ON q.id = ti.quiz_id
             WHERE t.quiz_id = :quiz_id 
               AND ti.quiz_id != t.quiz_id
               AND q.active = 1)
    ORDER BY RAND() LIMIT 1
    

    However you don't need that subselect at all you can just order by istag first:

    SELECT 
        IFNULL(q.meta_title, q.title) AS title, 
        IFNULL(url, title) AS url, 
        1 istag
    FROM tag_index t
    LEFT JOIN tag_index ti 
        ON ti.tag_id = t.tag_id
    LEFT JOIN quizzes q 
        ON q.id = ti.quiz_id
    WHERE t.quiz_id = :quiz_id 
        AND ti.quiz_id != t.quiz_id
        AND q.active = 1
    
    UNION ALL
    
    SELECT 
        IFNULL(q.meta_title, q.title) AS title, 
        IFNULL(url, title) AS url, 
        0 istag
    FROM category_index c
    LEFT JOIN category_index ci 
        ON ci.category_id = c.category_id
    LEFT JOIN quizzes q 
        ON q.id = ci.quiz_id
    WHERE c.quiz_id = :quiz_id 
        AND ci.quiz_id != c.quiz_id 
        AND q.active = 1
    ORDER BY istag DESC, RAND() LIMIT 1
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3