9

我正在编写一种旅行“约会”应用程序。

  • 用户自行注册
  • 用户告诉应用他们是男性还是女性
  • 用户告诉应用他们想访问哪些国家
  • 用户告诉应用他们想与男性 (pref_m=1) 还是女性 (pref_f=1) 一起旅行

我的桌子

表 1:用户

id (key) | gender | pref_m | pref_f
------------------------------------
 1          male     1        0
 2          male     1        1


表 2:国家选择

id (key) | userid | countryid
------------------------------------
 1          1        123
 2          1        111
 3          1        100
 4          1        110
 5          2        123
 6          2        111
 7          2        202
 8          2        210

那么select语句必须做什么

输入:当前用户的用户 ID
输出(在逻辑中):选择用户 ID 和匹配国家/地区的所有人,这些人想去和我一样的国家旅行,并想和我性别的人一起旅行(
加入)选择 我显然只需要与我正在寻找的性别相同的人。
由与我 DESC 最匹配的国家的人订购。

到目前为止我所拥有的(警告:不多)


$sql = "SELECT userid,count(*) AS matches from countryselection";
 $sql .= " WHERE countryid IN (SELECT countryid FROM countryselection WHERE userid = :userid) GROUP BY userid ORDER BY matches DESC;";
这给了我一份想要去和我一样的国家旅行的所有人的名单(以及我们有多少个共同国家)

最后注

我显然在性别选择部分苦苦挣扎。
不确定我是否做了正确的事情来以我的方式存储用户选择。
我可能也需要一些指导。


显然 - 谢谢大家。

4

6 回答 6

8
SELECT        
    us2.id, -- etc. 
    COUNT(cs2.countryid) as countries_in_common               
FROM
    countryselection cs1 -- let's gather user countries he want to visit
LEFT JOIN -- now let's find other users!
    countryselection cs2 ON
    (
        cs2.userid <> :userid AND -- which are not him
        cs2.countryid = cs1.countryid -- and want to visit same countries
    )
INNER JOIN -- let's grab our user_data
    users us1 ON 
    (
        us1.id = cs1.userid
    )
INNER JOIN -- and let's grab other user data!
    users us2 ON
    (
        us2.id = cs2.userid
    )
WHERE
    cs1.userid = :userid AND -- finding our user countries he want to visit
    -- final checks
    (
        (us1.pref_m = 1 AND us2.gender = 'male') 
        -- he is looking for male and second user is male
        OR
        (us1.pref_f = 1 AND us2.gender = 'female') 
        -- he is looking for female and second user is female
    ) AND
    (
        (us2.pref_m = 1 AND us1.gender = 'male')
        OR
        (us2.pref_f = 1 AND us1.gender = 'female') 
    ) 
GROUP BY
    cs2.userid -- finally group by user_id

最好的是没有子查询,您可以通过多种方式轻松使用此查询。(更改顺序、分组依据和使用聚合函数)

于 2013-01-27T23:04:17.510 回答
2

如果您不按大多数国家/地区共同进行排序,这很容易(如果结果集不会太大,您可以稍后在代码中进行):

SELECT
    o.id userid, u_cs.countryid
FROM users u
JOIN countryselection u_cs ON (u.id = u_cs.userid)
JOIN countryselection o_cs ON (u_cs.countryid = o_cs.countryid)
JOIN users o ON (o_cs.userid = o.id)
WHERE
    u.id = :userid AND   -- The user we want
    u.id <> o.id AND     -- Exclude ourselves
    (                    -- Check whether the other person is
                         -- compatible with us
        (u.pref_m = 1 AND o.gender = 'male') OR
        (u.pref_f = 1 AND o.gender = 'female')
    ) AND
    (                    -- Check whether we're compatible with the
                         -- other person
        (o.pref_m = 1 AND u.gender = 'male') OR
        (o.pref_f = 1 AND u.gender = 'female')
    )

SQL小提琴


如果您确实想要排序,我认为最好的选择是使用GROUP_CONCAT(因为 MySQL 很烂并且不支持窗口/分析功能)。

SELECT
    o.id userid, GROUP_CONCAT(u_cs.countryid) countries
FROM users u
JOIN countryselection u_cs ON (u.id = u_cs.userid)
JOIN countryselection o_cs ON (u_cs.countryid = o_cs.countryid)
JOIN users o ON (o_cs.userid = o.id)
WHERE
    u.id = :userid AND   -- The user we want
    u.id <> o.id AND     -- Exclude ourselves
    (                    -- Check whether the other person is
                         -- compatible with us
        (u.pref_m = 1 AND o.gender = 'male') OR
        (u.pref_f = 1 AND o.gender = 'female')
    ) AND
    (                    -- Check whether we're compatible with the
                         -- other person
        (o.pref_m = 1 AND u.gender = 'male') OR
        (o.pref_f = 1 AND u.gender = 'female')
    )
GROUP BY o.id
ORDER BY COUNT(u_cs.countryid) DESC

你也可以用一些讨厌的子查询来解决这个问题,但我觉得它会影响性能。

SQL小提琴

于 2013-01-27T23:37:53.557 回答
2
SELECT t4.id, COUNT(t4.id) AS frequency
FROM users t1
LEFT JOIN countryselection t2
ON t1.id = t2.userid
INNER JOIN countryselection t3 
  ON t2.userid != t3.userid AND t2.countryid = t3.countryid
INNER JOIN users t4 
  ON t3.userid = t4.id 
   AND ((t4.pref_m = 1 AND t1.gender = 'male' OR t4.pref_f = 1 AND t1.gender = 'female')
     AND (t1.pref_m = 1 AND t4.gender = 'male' OR t1.pref_f = 1 AND t4.gender = 'female'))
WHERE t1.id = ?
GROUP BY t4.id
ORDER BY frequency DESC

与此处的其他人类似,但使用适当的连接类型和连接条件而不是 where 条件。

来自MySQL 文档

与 ON 一起使用的 conditional_expr 是可以在 WHERE 子句中使用的任何形式的条件表达式。通常,您应该对指定如何连接表的条件使用 ON 子句,并使用 WHERE 子句来限制结果集中需要哪些行。

于 2013-01-28T00:03:09.760 回答
1

推断 DDL:

create table users (id int, gender text, pref_m bool, pref_f bool);
create table countryselection (id int, userid int, countryid int);

这是您可以.import(使用 sqlite3,在做之后.separator ,)进入问题中的表的 CSV:

用户.csv:

1,male,1,0
2,male,1,1

国家选择.csv

1,1,123
2,1,111
3,1,100
4,1,110
5,2,123
6,2,111
7,2,202
8,2,210

彼得的 sql,编辑为使用问题中的字段名称:

SELECT        
    us2.id,
    COUNT(cs2.*) as countries_in_common
FROM
    countryselection cs1 
LEFT JOIN 
    countryselection cs2 ON
    (
        cs2.userid <> $userid AND 
        cs2.countryid = cs1.countryid 
    )
LEFT JOIN 
    users us1 ON 
    (
        us1.id = cs1.userid
    )
LEFT JOIN 
    users us2 ON
    (
        us2.id = cs2.userid
    )
WHERE
    cs1.userid = $userid AND 
    cs2.userid IS NOT NULL AND
    (
        (us1.pref_m = 1 AND us2.gender = 'male') 
        OR
        (us1.pref_f = 1 AND us2.gender = 'female') 
    ) AND
    (
        (us2.pref_m = 1 AND us1.gender = 'male')
         OR
        (us2.pref_f = 1 AND us1.gender = 'female') 
    )          
GROUP BY
    cs2.userid 
;

你可以像这样执行它:

sqlite3 myDBname < peters_sql.sql

设置$userid1,我得到2输出。

于 2013-01-27T23:22:00.330 回答
1

我认为这行得通

select me.id meid
     , them.id themid
     , me.gender mygender
     , them.gender themgender
     , me.pref_m mepref_m
     , me.pref_f mepref_f
     , them.pref_m thempref_m
     , them.pref_f thempref_f
     , csme.countryid

  from users me

 cross
  join users them

 inner
  join countryselection csme
    on me.id = csme.userid

 inner
  join countryselection csthem
    on them.id = csthem.userid

 where csme.countryid = csthem.countryid
   and ((me.gender = 'male' and them.pref_m) or (me.gender = 'female' and them.pref_f))
   and ((them.gender = 'male' and me.pref_m) or (them.gender = 'female' and me.pref_f))
   and me.id != them.id
   and me.id = 2

http://sqlfiddle.com/#!2/06351/25/0

我故意将分组排除在外,以便更容易验证结果。

于 2013-01-27T23:43:12.740 回答
1

更新:(添加国家)

SELECT u1.id AS uid1
        , u2.id AS uid2
        , cs.countryid
FROM users u1
, users u2
JOIN countryselection cs ON cs.userid = u2.id
-- WHERE u1.id < u2.id -- tiebreaker
WHERE u1.id = 12345
AND EXISTS (
        SELECT * FROM countryselection cs1
        JOIN countryselection cs2 ON cs1.countryid = cs2.countryid
        WHERE cs1.userid = u1.id
        AND cs2.userid = u2.id
        )
AND ((u1.pref_m = True AND u2.gender = 'male')
        OR (u1.pref_f = True AND u2.gender = 'female') )
        -- the love must be mutual ...
AND ((u2.pref_m = True AND u1.gender = 'male')
        OR (u2.pref_f = True AND u1.gender = 'female') )
        ;
于 2013-01-27T23:48:19.683 回答