【问题标题】:Randomised MySQL Query Validation against another table针对另一个表的随机 MySQL 查询验证
【发布时间】:2020-11-17 23:49:57
【问题描述】:

我有 3 张桌子:

  1. “用户”列表 (uID)
  2. “问题”列表 (qID)
  3. “questionsanswered”表存储哪些问题已发送给哪些用户(qaUID 和 qaQID)

我需要做一个随机查询,我选择一个随机问题和一个随机用户,但不允许选择以前提出的问题,因此它需要根据 questionsanswered 表进行验证。

你能帮忙吗?

【问题讨论】:

    标签: mysql join random


    【解决方案1】:

    您可以cross joinusersquestions 表,并使用not exists 过滤掉questionsanswered 中存在的元组。那么剩下要做的就是随机选择一行。

    select u.uid, q.qid
    from users u
    cross join questions q 
    where not exists (
        select 1 
        from questionsanswered qa 
        where qa.qauid = u.uid and quqid = q.quid
    )
    order by rand()
    limit 1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多