简体   繁体   English

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

[英]mysql query logic for fetching data from 3 related tables on certain condition

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(为简单起见)

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

structure of 'problem' table: problem_id, problem_name (for simplicity) 'problem'表的结构:problem_id,problem_name(为简单起见)

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

structure of 'relation' table: rel_id, contest_id, problem_id 'relation'表的结构: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'. 我计划允许管理员设置系统一次,然后有他/她想要的竞赛,所以同样的'problem_id'可以分配给多个'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: 对于单个比赛,我正在使用此查询获取该问题的所有'problem_id'以及该问题的所有内容:

 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. 我可以在php中做同样的事情,使用两个循环,一个用于遍历整个系统中的所有'problem_id'和循环内部,另一个循环用于遍历该竞赛的所有'problem_id',反之亦然。 But it'll cost me an O(n^2) complexity which I'm sure can be avoided using mysql query. 但它会花费我O(n ^ 2)的复杂度,我相信使用mysql查询可以避免。 Any idea to do it in php more efficiently will be also good for me. 任何想法在php中更高效地做这件事对我来说也会有好处。 Any help is appreciated. 任何帮助表示赞赏。

You can use MYSQL NOT IN() to remove the problems that are already in the relation table for the selected contest . 您可以使用MYSQL NOT IN()来删除所选contestrelation表中已有的问题。

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. NOT IN()确保表达式继续没有参数中存在的任何值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM