我对这个问题感到非常疯狂,想知道是否有一个天才,对他来说这只是一个孩子的游戏......?
此查询应建议随机用户登录用户,而登录用户和建议用户之间的关系不得以下列方式之一被阻止:
- 按大洲
- 按国家
- 按年龄段
- 按性别
- 按 id (blocker<->blockee)
表结构
用户
id | username | country_id_home | timestamp_lastonline | gender | birthdate | allow_gender | age_min | age_max | ...
注释:birthdate
是 mysql-date 格式 ( YYYY-MM-DD
), gender
(0=female,1=male), allow_gender
(0=both, 1=female, 2=male)
国家
country_id | name | continent_id | ...
城市
id | city_real | ...
评论:loc_id_home
在表中user
匹配id
cities
封锁国家
id | user_id | country_id | ...
被封锁的大陆
id | user_id | continent_id | ...
阻塞用户
id | user1_id | user2_id | ...
评论:user1 是“blocker”,user2 是“blockee”
我到目前为止的查询是:
SELECT u.id AS user_id, u.username, u.fname, u.country_id_home, u.timestamp_reg, u.timestamp_upd, u.timestamp_lastonline, u.online_status, u.gender, u.birthdate, u.prof_pic,
(SELECT name FROM countries co WHERE co.country_id=u.country_id_home) AS country_name,
(SELECT city_real FROM cities ci WHERE ci.id=u.loc_id_home) AS city ,
(SELECT region_name FROM regions r WHERE r.id=u.region_id_home) AS region,
(SELECT region_short FROM regions r WHERE r.id=u.region_id_home) AS region_short
FROM user_d1 u
LEFT JOIN (
SELECT DISTINCT user2_id AS blockee_id
FROM blocked_user
WHERE user1_id = :user1_id1
)this_user_blocked ON u.id = this_user_blocked.blockee_id
LEFT JOIN (
SELECT DISTINCT user1_id AS blocker_id
FROM blocked_user
WHERE user2_id = :user1_id2
)blocked_this_user ON u.id = blocked_this_user.blocker_id
WHERE
(allow_gender=0 OR allow_gender=:user1_allow_gender)
AND age_min<=:user1_age1
AND age_max>=:user1_age2
AND prof_status<>2
AND this_user_blocked.blockee_id IS NULL AND blocked_this_user.blocker_id IS NULL
AND NOT EXISTS (SELECT 1 FROM blocked_countries bc WHERE bc.user_id=u.id AND bc.country_id =:user1_country)
AND NOT EXISTS (SELECT 1 FROM blocked_countries bc WHERE bc.user_id=:user1_id3 AND bc.country_id = u.country_id_home)
AND EXISTS (
SELECT 1 FROM user_d1 u2
WHERE u2.id=:user1_id4
AND (u2.allow_gender=0 OR u2.allow_gender=(u.gender+1))
AND age_min<=(DATEDIFF(NOW(),u.birthdate))
AND age_max>=(DATEDIFF(NOW(),u.birthdate))
AND prof_status<>2
)
LIMIT 0,6
没有最后AND EXISTS...
一个结果集不是空的,但是像这样它总是不返回任何结果。然而被大陆封锁不包括在内,它还没有被timestamp_lastonline
...订购
我最后做错了AND EXISTS...
什么?应该用 a 来完成JOIN
吗?我也试过了,但它也给出了一个空的结果......
我已将登录用户的所有必要变量存储在 SESSION-variables 中......如果您在查询中发现可以以更好的方式完成的任何其他内容,我也很乐意为您提供帮助!
非常感谢您!