如何结合这两个MySQL语句

时间:2012-05-30 18:27:08

标签: mysql wordpress buddypress

我正在使用这两个语句来查询BuddyPress的友情,但我想知道它们是更好的方式还是组合这两个MySQL语句的方法:

    //Statement for friendship initiator
    $SQL1 = "SELECT wp_users.display_name AS'name', 
                    wp_users.user_login AS 'fname', 
                    wp_users.user_nicename AS 'surname' 
            FROM wp_users 
            INNER JOIN wp_bp_friends 
                  ON wp_users.id = wp_bp_friends.friend_user_id 
            WHERE 1=1 
               AND wp_bp_friends.initiator_user_id = " . $user_id . " 
               AND wp_bp_friends.is_confirmed = 1";


    //Statement for friendship non-initiator
    $SQL2 = "SELECT wp_users.display_name AS 'name', 
                    wp_users.user_login AS 'fname', 
                    wp_users.user_nicename AS 'surname' 
             FROM wp_users 
             INNER JOIN wp_bp_friends 
                   ON wp_users.id = wp_bp_friends.initiator_user_id 
             WHERE 1=1 
                   AND wp_bp_friends.friend_user_id = " . $user_id . " 
                   AND wp_bp_friends.is_confirmed = 1";

结果应包含基于$ user_id的友谊列表,无论他们是否发起了这种关系。

3 个答案:

答案 0 :(得分:2)

$sql = "
  SELECT u.display_name  AS name,
         u.user_login    AS fname,
         u.user_nicename AS surname
  FROM   wp_users        AS u
    JOIN wp_bp_friends   AS f
      ON (u.id, $user_id) IN (
           (f.friend_user_id,    f.initiator_user_id),
           (f.initiator_user_id, f.friend_user_id)
         )
  WHERE  f.is_confirmed = 1
";

修改

下面的每@Quassnoi's comment,与上面的答案一样优雅,它不会使用索引进行查找(由于MySQL实现缺陷)。相反,你可以这样做:

$sql = "
  SELECT u.display_name  AS name,
         u.user_login    AS fname,
         u.user_nicename AS surname
  FROM   wp_users        AS u
    JOIN wp_bp_friends   AS f
      ON (f.friend_user_id    = u.id AND f.initiator_user_id = $user_id)
      OR (f.initiator_user_id = u.id AND f.friend_user_id    = $user_id)
  WHERE  f.is_confirmed = 1
";

答案 1 :(得分:0)

如果您不关心哪个结果来自哪个查询,那么您可以轻松地将它们与UNION结合使用,如下所示:

$sql = "SELECT 
   wp_users.display_name AS 'name', 
   wp_users.user_login AS 'fname', 
   wp_users.user_nicename AS 'surname' 
FROM wp_users 
INNER JOIN wp_bp_friends ON wp_users.id = wp_bp_friends.friend_user_id 
WHERE 1=1 
AND wp_bp_friends.initiator_user_id = " . $user_id . " 
AND wp_bp_friends.is_confirmed = 1
UNION
SELECT 
   wp_users.display_name AS 'name', 
   wp_users.user_login AS 'fname', 
   wp_users.user_nicename AS 'surname' 
FROM wp_users 
INNER JOIN wp_bp_friends ON wp_users.id = wp_bp_friends.initiator_user_id 
WHERE 1=1 
AND wp_bp_friends.friend_user_id = " . $user_id . " 
AND wp_bp_friends.is_confirmed = 1";

如果它们来自哪个查询并不重要,那么您可以为每个查询添加一个静态varchar列,稍后您可以使用它来确定该行来自哪个查询:

$sql = "SELECT 'FIRST', 
   wp_users.display_name AS 'name', 
   wp_users.user_login AS 'fname', 
   wp_users.user_nicename AS 'surname' 
FROM wp_users 
INNER JOIN wp_bp_friends ON wp_users.id = wp_bp_friends.friend_user_id 
WHERE 1=1 
AND wp_bp_friends.initiator_user_id = " . $user_id . " 
AND wp_bp_friends.is_confirmed = 1
UNION
SELECT 'SECOND',
   wp_users.display_name AS 'name', 
   wp_users.user_login AS 'fname', 
   wp_users.user_nicename AS 'surname' 
FROM wp_users 
INNER JOIN wp_bp_friends ON wp_users.id = wp_bp_friends.initiator_user_id 
WHERE 1=1 
AND wp_bp_friends.friend_user_id = " . $user_id . " 
AND wp_bp_friends.is_confirmed = 1";

答案 2 :(得分:0)

SELECT  u.display_name, u.user_login, u.user_nicename
FROM    wp_bp_friends f
JOIN    wp_users u
ON      u.id =
        CASE @userId
        WHEN f.initiator_user_id THEN
                f.friend_user_id
        WHEN f.friend_user_id THEN
                f.initiator_user_id
        END
WHERE   (
        f.initiator_user_id = @userId
        OR
        f.friend_user_id = @userId
        )
        AND f.is_confirmed = 1

initiator_user_idfriend_user_id上创建索引。

由于对index_merge的更多顺序访问,这将使用InnoDBUNION)(wp_bp_friends}比两个结果集的{{1}}略微更有效。