简体   繁体   中英

Union 2 column and another column from another table

for my friendship script my database looks like this :

   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 

with this query:

   `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`

I got an array which has all info about accepted friendship. here the 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...

But at the same time i would like to merge this results with user profile pictures. thats why i wrote this query but it doesnt work. where am i missing?

$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(); 

i would like to have an array like this and DESC by time.

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

When you UNION query results, the columns need to be the same in every result set. What you are looking for in this scenario is the combination of a JOIN to get the user profile picture and then a UNION to get the "from" and "to" friends.

  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 

You need JOIN to merge two tables of different types on a common column

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM