/* this would get the user */
SELECT *
FROM users
WHERE id = $ID
/* build on this to get relationships */
SELECT *
FROM users u
JOIN relationship r ON r.user_id = u.id
WHERE u.id = $ID
/* build on this to get not blocked */
SELECT *
FROM users u
JOIN relationship r ON r.user_id = u.id
JOIN relationship_block b ON b.user_id = u.id
WHERE u.id = $ID
AND r.member_ID <> b.blocked_member_id
/* get all users that NO ONE has blocked */
/* this means if there exists a record b such that b.blocked_member_id
equals the user X has blocked user Y, do not include user Y.
By extension, if X and Y are fierce enemies and have blocked eachother,
neither would get returned by the query */
SELECT *
FROM users u
JOIN relationship r ON r.id = u.id
WHERE NOT EXISTS (SELECT null
FROM relationship_block rb
WHERE rb.blocked_member_id = u.id
)
/* This runs two queries at once. The inner query says "I'm not getting any
columns, because I don't care about the actual data, I just to get all
records where someone has blocked the user I'm currently looking for".
Then you select all users where that isn't true. For good speed, this would
require an index on relationship_block.blocked_member_id */
作爲一個側面說明,不`SELECT *`...這是僅供參考! – corsiKa 2011-02-17 19:37:40