如何获取帖子列表而不会阻止用户在MySQL中发帖?

时间:2017-06-05 06:07:41

标签: mysql sql join

我想获得一个没有阻止用户使用MySQL 5.7发布的帖子列表。 我的CREATE语句如下所示:

CREATE TABLE `friends` (
`friend_one` INT(11) NOT NULL,
`friend_two` INT(11) NOT NULL,
`status` INT(11) UNSIGNED NULL DEFAULT NULL,
`requested_id` INT(11) UNSIGNED NOT NULL,
`last_status` INT(11) UNSIGNED NULL DEFAULT NULL,
`blocked_by` INT(11) UNSIGNED NULL DEFAULT NULL,
`time` VARCHAR(50) NULL DEFAULT NULL COLLATE 'utf8mb4_bin',
UNIQUE INDEX `friend_one_friend_two` (`friend_one`, `friend_two`));

CREATE TABLE `post` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`user_id_fk` INT(11) NULL DEFAULT NULL,
`user_id` VARCHAR(50) NOT NULL COLLATE 'utf8mb4_bin',
`type` TINYINT(4) UNSIGNED NOT NULL DEFAULT '0',
`lat` VARCHAR(50) NOT NULL COLLATE 'utf8mb4_bin',
`lng` VARCHAR(50) NOT NULL COLLATE 'utf8mb4_bin',
`flagged` BIT(1) NULL DEFAULT b'0',
`checked` BIT(1) NULL DEFAULT b'0',
PRIMARY KEY (`id`));

问题是,如果我运行这个sql语句,我只会在没有其他正常用户帖子的情况下发布我的FRIENDS帖子。我希望获得其他用户发布的所有帖子列表,而不是我的朋友(或用户)阻止。

SELECT d.id, d.user_id, d.user_id_fk, d.type, d.flagged, d.checked, d.lat, d.lng, d.version, f.friend_one, f.friend_two, f.`status`, f.requested_id, f.last_status, f.blocked_by
FROM (
    SELECT  z.id,
            z.user_id_fk,
            z.user_id,
            z.type,
            z.flagged,
            z.checked,
            z.version,
            p.radius,
            p.distance_unit
         * DEGREES(ACOS(COS(RADIANS(p.latpoint))
         * COS(RADIANS(z.lat))
         * COS(RADIANS(p.longpoint - z.lng))
         + SIN(RADIANS(p.latpoint))
         * SIN(RADIANS(z.lat)))) AS distance,
         z.lat,
         z.lng
    FROM post AS z
    JOIN (
            SELECT 37.448758 AS latpoint, 126.451617 AS longpoint,
                 0.24 AS radius, 111.045 AS distance_unit
    ) AS p ON 1 = 1
    LEFT JOIN blocked_posts bp ON z.id = bp.post_id AND bp.user_id = "auth0|586e14f70b097f12dd78eebb"
  WHERE z.lat
     BETWEEN p.latpoint  - (p.radius / p.distance_unit)
         AND p.latpoint  + (p.radius / p.distance_unit)
         AND z.lng
     BETWEEN p.longpoint - (p.radius / (p.distance_unit * COS(RADIANS(p.latpoint))))
         AND p.longpoint + (p.radius / (p.distance_unit * COS(RADIANS(p.latpoint))))
     AND z.type != 2
     AND z.flagged = (0)
     AND bp.post_id IS NULL
) AS d
JOIN friends f ON (f.friend_one = 401 AND f.friend_two = d.user_id_fk) OR (f.friend_two = 401 AND f.friend_one = d.user_id_fk)
WHERE distance <= radius 

我认为加入表格存在问题。有没有办法实现这个目标?

0 个答案:

没有答案