3

私は新しいプロジェクトを開発しています。mysql クエリのヘルプが必要です。非常に遅いと思います。

SELECT post_id, user_id, content, title, TIME, (

SELECT username
FROM users
WHERE id = posts.user_id
) AS username
FROM posts
WHERE user_id =1
OR user_id
IN (

SELECT DISTINCT user_to
FROM friends
WHERE user_from =1
OR user_to =1
)
ORDER BY posts.post_id DESC 
LIMIT 0 , 10 

助けてくれてありがとう。

4

3 に答える 3

4

これを試してみてください -

SELECT  
    p.post_id
  , p.user_id
  , p.content
  , p.title
  , p.`time`
  , u.username
FROM posts AS p
/*LEFT*/ JOIN users AS u ON u.ID = p.user_id
WHERE user_id = 1
    OR user_id IN (
        SELECT DISTINCT user_to
        FROM friends
        WHERE user_from = 1
          OR user_to = 1
    )
ORDER BY p.post_id DESC
LIMIT 0, 10 
于 2013-06-11T07:28:05.957 に答える
2

試す:

SELECT p.post_id, p.user_id, p.content, p.title, p.TIME, u.username
FROM (select distinct us.* 
      from friends f
      join users us on f.user_to = us.id
      WHERE f.user_from =1 OR f.user_to =1) u
JOIN posts p on u.id = p.user_id
ORDER BY p.post_id DESC 
LIMIT 0 , 10 
于 2013-06-11T09:10:30.237 に答える