(
    /* 自分のUpdate */
    SELECT *
    FROM statuses
    WHERE senderId    = /*userId*/0 AND
          sentTime    > /*sentTime*/0 AND
          removedTime = 0
    ORDER BY statusId DESC
) /*IF repliesType.always*/ UNION ALL (
    /* Followしている人の（すべてのReplieを含む）Update */
    SELECT c.*
    FROM users       AS a,
         friendships AS b,
         statuses    AS c
    WHERE a.userId      = b.passiveId AND
          b.passiveId   = c.senderId AND
          a.protect     = true AND
          b.activeId    = /*userId*/0 AND
          b.accept      = true AND
          b.removedTime = 0 AND
          c.sentTime    > /*sentTime*/0 AND
          c.removedTime = 0
    ORDER BY c.statusId DESC
) UNION ALL (
    SELECT c.*
    FROM users       AS a,
         friendships AS b,
         statuses    AS c
    WHERE a.userId      = b.passiveId AND
          b.passiveId   = c.senderId AND
          a.protect     = false AND
          b.activeId    = /*userId*/0 AND
          b.removedTime = 0 AND
          c.sentTime    > /*sentTime*/0 AND
          c.removedTime = 0
    ORDER BY c.statusId DESC
) -- ELSE UNION ALL (
    /* Followしている人の（Repliesを除く）Update */
    SELECT c.*
    FROM users       AS a,
         friendships AS b,
         statuses    AS c
    WHERE a.userId      = b.passiveId AND
          b.passiveId   = c.senderId AND
          a.protect     = true AND
          b.activeId    = /*userId*/0 AND
          b.accept      = true AND
          b.removedTime = 0 AND
          c.referenceSenderId = 0 AND
          c.sentTime    > /*sentTime*/0 AND
          c.removedTime = 0
    ORDER BY c.statusId DESC
) UNION ALL (
    SELECT c.*
    FROM users       AS a,
         friendships AS b,
         statuses    AS c
    WHERE a.userId      = b.passiveId AND
          b.passiveId   = c.senderId AND
          a.protect     = false AND
          b.activeId    = /*userId*/0 AND
          b.removedTime = 0 AND
          c.referenceSenderId = 0 AND
          c.sentTime    > /*sentTime*/0 AND
          c.removedTime = 0
    ORDER BY c.statusId DESC
) /*IF repliesType.friends*/ UNION ALL (
    /* Followしている人の（Followしている人宛の）Replies */
    SELECT c.*
    FROM users       AS a,
         friendships AS b,
         statuses    AS c,
         users       AS d,
         friendships AS e
    WHERE a.userId      = b.passiveId AND
          b.passiveId   = c.senderId AND
          c.referenceSenderId = d.userId AND
          d.userId      = e.passiveId AND
          a.protect     = true AND
          b.activeId    = /*userId*/0 AND
          b.accept      = true AND
          b.removedTime = 0 AND
          c.sentTime    > /*sentTime*/0 AND
          c.removedTime = 0 AND
          d.protect     = true AND
          e.activeId    = /*userId*/0 AND
          e.accept      = true AND
          e.removedTime = 0
    ORDER BY c.statusId DESC
) UNION ALL (
    SELECT c.*
    FROM users       AS a,
         friendships AS b,
         statuses    AS c,
         users       AS d,
         friendships AS e
    WHERE a.userId      = b.passiveId AND
          b.passiveId   = c.senderId AND
          c.referenceSenderId = d.userId AND
          d.userId      = e.passiveId AND
          a.protect     = true AND
          b.activeId    = /*userId*/0 AND
          b.accept      = true AND
          b.removedTime = 0 AND
          c.sentTime    > /*sentTime*/0 AND
          c.removedTime = 0 AND
          d.protect     = false AND
          e.activeId    = /*userId*/0 AND
          e.removedTime = 0
    ORDER BY c.statusId DESC
) UNION ALL (
    SELECT c.*
    FROM users       AS a,
         friendships AS b,
         statuses    AS c,
         users       AS d,
         friendships AS e
    WHERE a.userId      = b.passiveId AND
          b.passiveId   = c.senderId AND
          c.referenceSenderId = d.userId AND
          d.userId      = e.passiveId AND
          a.protect     = false AND
          b.activeId    = /*userId*/0 AND
          b.removedTime = 0 AND
          c.sentTime    > /*sentTime*/0 AND
          c.removedTime = 0 AND
          d.protect     = true AND
          e.activeId    = /*userId*/0 AND
          e.accept      = true AND
          e.removedTime = 0
    ORDER BY c.statusId DESC
) UNION ALL (
    SELECT c.*
    FROM users       AS a,
         friendships AS b,
         statuses    AS c,
         users       AS d,
         friendships AS e
    WHERE a.userId      = b.passiveId AND
          b.passiveId   = c.senderId AND
          c.referenceSenderId = d.userId AND
          d.userId      = e.passiveId AND
          a.protect     = false AND
          b.activeId    = /*userId*/0 AND
          b.removedTime = 0 AND
          c.sentTime    > /*sentTime*/0 AND
          c.removedTime = 0 AND
          d.protect     = false AND
          e.activeId    = /*userId*/0 AND
          e.removedTime = 0
    ORDER BY c.statusId DESC
)/*END*//*END*/
ORDER BY statusId DESC
LIMIT 0, /*limit*/1
