多个表和查询

时间:2011-11-30 11:35:49

标签: php mysql

我如何将这样的东西加在一起:

$query = ("SELECT * FROM profiles,follow
           WHERE follow.friend ='$user' 
               AND follow.user !='$user' 
               AND profiles.user1 =follow.user 
           ORDER BY id DESC limit 20");

("SELECT * FROM profiles WHERE user1='$user'");

我需要查询显示两个不同的东西我该怎么做。

3 个答案:

答案 0 :(得分:1)

这一个?

SELECT
    *
FROM
    follows
INNER JOIN
    profiles
    ON (profiles.user1 = follow.user)
WHERE
    follow.friend = '{$user}'
    AND follow.user != '{$user}'
    AND profiles.user1 = '{$user}'
ORDER BY
    profiles.id DESC
LIMIT 20

答案 1 :(得分:0)

试试这个

$query = "SELECT * FROM profiles,follow WHERE follow.friend ='$user' and follow.user !='$user' and profiles.user1 =follow.user and profiles.user1='$user' order by id DESC limit 20";

答案 2 :(得分:0)

使用UNION:

SELECT * 
FROM profiles, follow
WHERE follow.friend ='$user' 
  AND follow.user !='$user' 
  AND profiles.user1 =follow.user 
  ORDER BY id DESC limit 20
UNION
SELECT * 
FROM profiles 
WHERE user1='$user'