Union 2列和另一个表中的另一列

时间:2014-05-14 00:56:59

标签: php mysql sql

对于我的友谊脚本我的数据库看起来像这样:

   friends table                                  users table

   |from_id | to_id | accepted |              |user_id | user_profile_picture |
   -----------------------------              --------------------------------
   |   1    |  3    |  1                      |   1    | 1_profie.jpg 
   |   2    |  5    |  1                      |   2    | 2_profie.jpg 
   |   1    |  5    |  1                      |   3    | 3_profie.jpg 
   |   1    |  2    |  1                      |   4    | 4_profie.jpg 
   |   9    |  1    |  1                      |   5    | 5_profie.jpg 
   |   8    |  1    |  0                      |   9    | 9_profie.jpg 

使用此查询:

   `SELECT from_id AS friend_id , created_date AS time FROM friends 
    WHERE to_id = :user_id1 AND accepted = 1 
    UNION SELECT to_id  AS friend_id , created_date  AS time FROM friends 
    WHERE from_id = :user_id2 AND accepted = 1 ORDER BY time DESC`

我有一个数组,其中包含有关已接受友谊的所有信息。这里是var_dump

array (size=4)
0 => 
  array (size=2)
  'friend_id' => int 9
  'time' => string '2014-05-13 16:36:32' (length=19)
1 => 
  array (size=2)
  'friend_id' => int 2
  'time' => string '2014-05-13 16:16:12' (length=19)
  etc...

但与此同时我想将此结果与用户个人资料图片合并。这就是为什么我写这个查询但它不起作用。我错过了哪里?

$query = $database->connection->prepare("

      SELECT f.from_id AS friend_id , f.created_date AS time FROM friends f 
      WHERE f.to_id = :user_id1 AND f.accepted = 1 
      UNION SELECT t.to_id  AS friend_id , t.created_date  AS time FROM friends t 
      WHERE t.from_id = :user_id2 AND t.accepted = 1 
      UNION SELECT u.user_profile_picture  AS friend_picture FROM users u 
      WHERE u.user_id = f.from_id OR u.user_id = t.to_id 
                                       "); 

$query->bindValue(':user_id1', $user_id, PDO::PARAM_STR); 
$query->bindValue(':user_id2', $user_id, PDO::PARAM_STR); 
$query->execute(); 

我希望有一个像这样的数组和DESC的时间。

 array (size=2)
  'friend_id' => int 2
  'time' => string '2014-05-13 16:16:12' (length=19)
  'profile_picture' => '2_profile.jpg'

2 个答案:

答案 0 :(得分:0)

当UNION查询结果时,每个结果集中的列必须相同。在这种情况下,您正在寻找的是JOIN的组合,以获取用户个人资料图片,然后使用UNION来获取“从”和“到”朋友。

  SELECT f.from_id AS friend_id, u.user_profile_picture AS profile_picture, f.created_date AS time 
  FROM friends f 
  INNER JOIN users u on u.user_id = f.from_id 
  WHERE f.to_id = :user_id1 AND f.accepted = 1 
  UNION 
  SELECT t.to_id  AS friend_id, u2.user_profile_picture AS profile_picture, t.created_date  AS time 
  FROM friends t 
  INNER JOIN users u2 on t.to_id = u2.user_id
  WHERE t.from_id = :user_id2 AND t.accepted = 1 

答案 1 :(得分:0)

您需要JOIN在公共列上合并两个不同类型的表

SELECT
    *
FROM
    users
    LEFT JOIN
    (
    SELECT f.from_id AS friend_id , f.created_date AS time FROM friends f 
    WHERE f.to_id = :user_id1 AND f.accepted = 1 
    UNION SELECT t.to_id  AS friend_id , t.created_date  AS time FROM friends t 
    WHERE t.from_id = :user_id2 AND t.accepted = 1 
    ) unioned  ON users.user_id = unioned.friend_id