0

我正在使用以下查询从我的数据库中获取有关聚会(活动)的信息。我希望能够选择 4 个用户(参与活动的)个人资料图片。

SELECT
    m.meetup_name,
    m.url_meetup,
    LEFT(m.meetup_text, 120) as teaser,
    DATE_FORMAT(m.datetime, "%d-%m-%Y <br> %I:%i") as datetime,
    a.username,
    a.url_username,
    c.city_name,
    (SELECT count(*) FROM meetup_participants mp WHERE MP.meetup_id = m.id) as participants
FROM
meetups m, users a, cities c
WHERE
    a.id = m.author_id
    AND c.postalcode = m.postalcode

我的表看起来像:

meetup =>
  id
  meetup_name
  url_meetup
  meetup_text
  author_id
  datetime
  ......

meetup_participants =>
  meetup_id
  user_id

users
  id
  username
  url_username
  profile_picture

我强烈希望不再使用任何子查询。我的第一个想法是我可以通过我已经拥有的子查询来做到这一点,但后来我发现我只能从子查询中返回一列。

我希望有人知道如何实现这一点。

4

2 回答 2

0

如果我正确理解您的表格,这应该会找到会议的前四名参与者。它通过meetup_participants解析表连接用户。

SELECT
  m.meetup_name,
  m.url_meetup,
  LEFT(m.meetup_text, 120) as teaser,
  DATE_FORMAT(m.datetime, "%d-%m-%Y <br> %I:%i") as datetime,
  a.username,
  a.url_username,
  c.city_name,
  mp.picture -- or whatever you need to get from meeting_participants
FROM meetups
INNER JOIN meetup_participants mp ON m.id = m.meetup_id
INNER JOIN users a ON mp.user_id = a.id
INNER JOIN cities c ON m.postalcode = c.postalcode
WHERE m.whatever = 'whatever identifies the event'
ORDER BY a.username -- or whatever is important to you, or don't ORDER the results
LIMIT 4
于 2013-05-28T14:15:21.140 回答
0

我最终在子查询中使用了 CONCAT,并使用 php 将字符串拆分为一个数组:

(SELECT GROUP_CONCAT(CONCAT(u.url_username,"/60_thumb/",u.profile_picture)) FROM meetup_participants mp, users u WHERE mp.meetup_id = m.id AND u.id = mp.user_id LIMIT 4) AS participants_pictures
于 2013-05-28T16:41:57.910 回答