dreamwind1985 2015-11-23 18:06
浏览 46
已采纳

mysql查询逻辑,用于在特定条件下从3个相关表中获取数据

I'm building a programming contest controller on web platform, which has some tables, including 'contest', 'problem' and 'relation' tables, which I'm having the trouble with.

structure of 'contest' table: contest_id, contest_name (for simplicity)

============================
|contest_id | contest_name |
|-----------|--------------|
|          1|  Test Contest|
|-----------|--------------|
|          2|   Another One|
============================

structure of 'problem' table: problem_id, problem_name (for simplicity)

============================
|problem_id | problem_name |
|-----------|--------------|
|          1|     A Problem|
|-----------|--------------|
|          2| Other Problem|
============================

structure of 'relation' table: rel_id, contest_id, problem_id

===========================================
|   rel_id  | contest_id   |  problem_id  |
|-----------|--------------|--------------|
|          1|             1|             1|
|-----------|--------------|--------------|
|          2|             1|             2|
|-----------|--------------|--------------|
|          3|             1|             8|
|-----------|--------------|--------------|
|          4|             2|             5|
|-----------|--------------|--------------|
|          5|             2|             8|
===========================================

I'm planning to allow the admin to set up the system once then have as many contests as s/he wants, so the same 'problem_id' can be assigned to multiple 'contest_id'.

For a single contest, I'm fetching all the 'problem_id's for that contest with all the content of that problem with this query:

SELECT * FROM `problem` JOIN `relation` on `relation`.`problem_id` = `problem`.`problem_id` WHERE `contest_id` = 3 // say the id is 3

But when editing the contest and adding some more problems in it, I need to fetch ONLY those problems to show which are NOT ALREADY in the same contest.

I tried this but didn't work, gave me some duplicates and other contest problems:

SELECT * FROM `problem` LEFT JOIN `relation` on `relation`.`problem_id` != `problem`.`problem_id` WHERE `contest_id` != 3

I can do the same thing inside php, using two loops, one for iterating through all the 'problem_id's in the whole system and inside that loop, another loop for iterating through all the 'problem_id's of that contest only, or vice versa. But it'll cost me an O(n^2) complexity which I'm sure can be avoided using mysql query. Any idea to do it in php more efficiently will be also good for me. Any help is appreciated.

</div>
  • 写回答

1条回答 默认 最新

  • douzhang1299 2015-11-23 19:09
    关注

    You can use MYSQL NOT IN() to remove the problems that are already in the relation table for the selected contest.

    SELECT * FROM problem WHERE 
        problem.problem_id NOT IN (SELECT problem_id FROM relation WHERE contest_id = 2)
    

    NOT IN() makes sure that the expression proceeded does not have any of the values present in the arguments.

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

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能