简体   繁体   English

从一个表中进行选择,而在另一个表中有两个条件不成立。 可能?

[英]SELECT from one table while two conditions are NOT TRUE in another table. Possible?

I'm stuck on my matchmaker site. 我停留在我的媒人网站上。 It's like eHarmony for married couples to find other couples to socialize with. 就像eHarmony,已婚夫妇找到其他可以与之交往的夫妇。

I'm trying to be a good rdba and am using a million tables then using a number of complicated joins to pull the info out I want. 我试图成为一个好的rdba,并且使用一百万张表,然后使用许多复杂的联接将信息提取出来。 But, I'm stuck on one of the last ones. 但是,我被困在最后一个。 I can do it in the code, but I should be able to do it in the SQL. 我可以在代码中做到这一点,但我应该能够在SQL中做到这一点。 Here's the deal: 这是交易:

I'm showing a member profile page with their list of matches. 我正在显示一个成员个人资料页面及其匹配列表。 (all the matchmaking algorithms are in and working). (所有配对算法都在运行中)。 You can then, give someone thumbs up or down. 然后,您可以竖起或竖起大拇指。 and it marks that in a "verdict" database. 并将其标记在“判定”数据库中。 I want to then refresh the member profile page and eliminate the voted on folks. 然后,我想刷新成员个人资料页面并消除已投票的成员。

Now, I'm only showing 4 matches, so I want you to be able to thumbs up someone or thumbs down them and then they disappear and are replaced by someone else. 现在,我只显示4个匹配项,所以我希望您能够使某人喜欢或不喜欢他们,然后它们消失并被其他人代替。

The problem is generating a sql statement that also checks the verdicts table. 问题是生成一个sql语句,该语句也检查verdicts表。

The hard part is that your id might be in one of two columns: the voter or the votee. 困难的部分是您的ID可能位于以下两列之一:投票者或被投票者。

SO, 

    I have these tables and I will list the columns that matter right now:

        couples 
        couples_id

        When a new person signs up, I recalculate the matches table,
    comparing every person with every other person and entering
    a compatibility quotient for each set of couples.:

        matches_couples
        matches_couples_couplea
        matches_couples_coupleb
        matches_couples_matchfactor
        (their compatibility number, I sort by this)

        When a person votes up or down on someone, 
    I enter a row for that vote.
 Who voted, about whom, and (a)ccepted or (r)ejected.:
        verdict_couples
        verdict_c_couplea (the person voting)
        verdict_c_coupleb (the person they're voting about)
        verdict_c_verdict (either 'r' for rejected or 'a' for accepted)

So, this is my current, working SQL: 因此,这是我当前正在使用的SQL:

SELECT
*

FROM
match_couples

WHERE
(match_couples_couple_a = '$couples_id'
or
match_couples_couple_b = '$couples_id')

ORDER BY
match_couples_matchfactor desc

LIMIT 4

But, it doesn't taken into account the voting, and will still show someone you rejected, or who has already rejected you or you approved. 但是,它没有考虑投票,仍然会显示您拒绝的人,或者已经拒绝您或您批准的人。 I want to strip out anyone who who has rejected you, or you rejected, or whom you approved. 我要剔除拒绝了您,拒绝了您或批准了您的任何人。

So basically, if you're EVER the verdict_c_couplea, I don't want to include the person who was the coupleb, since you've already made a decision about them. 因此,基本上,如果您曾经是verdict_c_couplea,由于您已经对它们做出了决定,所以我不想包括成对的人。

And if you're verdict_c_coubleb, and it's a 'r' for reject in verdict_c_verdict, I don't want to show that person either. 如果您是verdict_c_coubleb,并且在verdict_c_verdict中是拒绝的“ r”,我也不想显示该人。

SO, I want some super complicated JOIN or nested EXISTS clause or something that strips those people out (that way, my LIMIT 4 still works. 所以,我想要一些超级复杂的JOIN或嵌套的EXISTS子句,或将这些人剥离的东西(这样,我的LIMIT 4仍然可以使用。

IF NOT, the brute force method is to take off the limit, then for each of those people above, do a second SQL call to check the verdict table before letting them be part of the final list. 如果不是这样,那么强行方法就是取消限制,然后对上述每个人进行第二次SQL调用,以检查裁决表,然后再将其纳入最终列表。 But that's a major drag that I'm hoping to avoid. 但这是我希望避免的主要障碍。

I was able to get a COUNT on the number of times you approved a couple and they also approved you- a complete match. 我获得了您批准一对夫妇的次数,而他们也批准了您-完全匹配。 The answer to the above question, I think, is hiding in this working match count SQL but I can't even believe I got it to work: 我认为,上述问题的答案隐藏在此有效的匹配计数SQL中,但我什至不敢相信我可以使用它:

SELECT COUNT( * ) AS matches
FROM (
verdict_couples t1
)
JOIN (
verdict_couples same
) ON ( (
t1.verdict_c_couplea = same.verdict_c_coupleb
)
AND (
same.verdict_c_verdict = 'a'
)
AND (
t1.verdict_c_verdict = 'a'
) ) 
WHERE
    same.verdict_c_couplea = '$couples_id' 
    and 
   t1.verdict_c_coupleb = '$couples_id'

Basically the ON clause criss-crosses the WHERE clause, because you're looking for: 基本上,ON子句纵横交错于WHERE子句,因为您正在寻找:

id couplea coupleb verdict 54 US YOU accept 78 YOU US accept id couplea coupleb判决54美国您接受78您美国接受

That means we approved YOU and you approved US. 这意味着我们批准了您,而您批准了美国。 and amazingly that works. 令人惊讶的是 Somewhere in there is the guts to limit my matches list to just people I haven't voted on yet and who haven't rejected ME. 有胆量将我的比赛列表限制为我还没有投票,也没有拒绝我的人。

Once I figure this out, I'll replicate it for individual matches, as well. 一旦弄清楚了,我还将复制它以进行单独的匹配。

Any help on the joins? 对连接有任何帮助吗?

K ķ

SELECT *
FROM match_couples m
WHERE 
(m.match_couples_couple_a = '$couples_id'   # we are couple a
  AND m.matches_couples_coupleb NOT IN (    # couple b not in the list of couples which:
                                            # A. we have voted on before
                                        select verdict_c_coupleb 
                                        from verdict_couples
                                        where (verdict_c_couplea = $couples_id) 
                                        UNION
                                            # or B. have rejected us
                                        select verdict_c_couplea 
                                        from verdict_couples
                                        where (verdict_c_coupleb = $couples_id 
                                          AND verdict_c_verdict = 'r'))
OR
(m.match_couples_couple_b = '$couples_id'
  AND m.matches_couples_couplea NOT IN (select verdict_c_coupleb 
                                        from verdict_couples
                                        where (verdict_c_couplea = $couples_id) 
                                        UNION
                                        select verdict_c_couplea 
                                        from verdict_couples
                                        where (verdict_c_coupleb = $couples_id 
                                          AND verdict_c_verdict = 'r')
ORDER BY match_couples_matchfactor desc
LIMIT 4

暂无
暂无

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

相关问题 从两个表中各选择一个记录。 通过将另一个值与新表匹配来更新一个值 - select one record each from two table. update the one value by matching the another one to a new table ORDER BY NOT EXIST 在另一个表的两列中。 是否可以? - ORDER BY NOT EXIST in two column from another table. Is it possible? 从一张表中选择数据。 然后使用该数据从另一个表获取信息 - Select data from one table. Then using that data to get info from another table SQL-将一个表中多列的两行匹配到另一表中的1行。 - SQL - Matching two rows from multiple columns in one table to 1 row in another table. 从一个表中选择不在另一表中但具有特定条件的记录 - Select records from one table that are not in another table but with specific conditions MySQL-从表中选择两列匹配的行。 如果不存在,则选择具有一列的行 - MySQL - Selecting row with two columns match from a table. If doesn't exist select row with one column SQL从一个表中选择项目,从另一个表中选择条件 - SQL select items from one table with conditions from another 我尝试加入两个表,但另一个表名在第一个表的列中。 是否可以加入或是否有 - I try to join two table but another table name is in column of first table. Is it possible to join or is there is an 将数据从一个表复制到另一个表。 数据库不同,表结构不同 - Copying data from one table to another table. Databases are different and table structure is different SELECT * FROM table while condition=true? - SELECT * FROM table while condition=true?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM