0

我有一个包含帖子的表(edu_posts)。“post_receiver”字段通常包含一个 user_id,但有时它可能包含一个事件 ID(语法:event-ID),我创建了这样的语句:

  SELECT 
    p.*, 
    u.firstname AS post_author_firstname, 
    u.lastname AS post_author_lastname, 
    u3.firstname AS receiver_firstname, 
    u3.lastname AS receiver_lastname, 
    pl.like_author AS user_likes,
    CASE
    WHEN p.post_receiver REGEXP '^[a-z]'
    THEN
    SUBSTRING_INDEX(
      SUBSTRING_INDEX(p.post_receiver, '-', 2), 
      '-', 
      - 1
    )
    END
    AS event_id,
    e.event_name AS event_name
  FROM 
    edu_posts p 
    LEFT JOIN edu_users u ON u.user_id = p.post_author 
    LEFT JOIN edu_users u3 ON u3.user_id = p.post_receiver 
    LEFT JOIN edu_likes pl ON pl.like_entity = p.post_id
    LEFT JOIN edu_events e ON e.event_id = event_id

    AND pl.like_author = 1 
  GROUP BY 
    p.post_id, 
    pl.like_id 
  ORDER BY 
    p.post_date DESC

查看 SELECT 语句中的 CASE 和下面的内容,以及最后一个 LEFT JOIN。

我想我无法从我在 SELECT 语句中创建的“event_id”中检索数据。那么我可以根据 EVENT_ID 的值对 LEFT JOIN 做些什么呢?

也许整个过程太复杂了,相反,如果 post_receiver 等于:event-someID?

你觉得呢?你有没有什么想法?提前致谢!

4

0 回答 0