user11788312
user11788312

Reputation:

PDO union select results nothing

I need to select name, surname from users where username = column following inside network_follow table

the query:

"SELECT * FROM network_follow WHERE following = :f UNION SELECT name, surname FROM users where username = (following)";

php code:

 $f = $conexao_pdo->prepare('SELECT * FROM network_follow WHERE following = :f UNION SELECT name, surname FROM users where username = (following)');
    $f->bindParam(':f', $db_user);
    $f->execute();

    while($values = $f->fetch(PDO::FETCH_ASSOC)){
        $fvalue = $values['follower'];
        $fname = $values['name'];
        echo '<center> 
    <div><a href=""> <img class="rounded-circle"  width="45" src="'.get_user_picture($fvalue).'"></img>&nbsp;@'.$fvalue.'</a>
     <span style="padding-left: 15px;">'.$fname.'<span>
     <div></center>';

}

Resulting to me $fvalue just fine, but not $fname

for some reasons union select is not working, can somebody help?

Upvotes: 0

Views: 214

Answers (2)

rickdenhaan
rickdenhaan

Reputation: 11328

A UNION is used to select similar data from multiple tables.

A JOIN is used to combine related data from multiple tables.

Examples:

SELECT `city`, `sights` FROM `places_i_visited`
UNION
SELECT `city`, `sights` FROM `places_i_want_to_visit`

+----------------+--------+
| city           | sights |
+================+========+
| Los Angeles    | 1537   |  -- from `places_i_visited`
| Rio de Janeiro | 829    |  -- from `places_i_visited`
| Moscow         | 1822   |  -- from `places_i_want_to_visit`
+----------------+--------+

SELECT `city`, `sights`, `visits`.`date_visited`, `visits`.`duration`, `visits`.`sights_seen` FROM `places_i_visited`
INNER JOIN `visits` ON `visits`.`city_id` = `places_i_visited`.`city_id`

+----------------+--------+--------------+----------+-------------+
| city           | sights | date_visited | duration | sights_seen |
+================+========+==============+==========+=============+
| Los Angeles    | 1537   | 2017-06-25   | 14       | 25          |
| Rio de Janeiro | 829    | 2018-11-04   | 7        | 12          |
+----------------+--------+--------------+----------+-------------+

In your case, you want to take information from your network_following table and combine it with the user's name from the users table. A JOIN is more appropriate here:

SELECT network_follow.*, users.name, users.surname FROM network_follow
INNER JOIN users ON users.username = network_follow.following
WHERE following = :f

I'm using an INNER JOIN here to make sure you only see entries that have results in both tables.

If you want to see if you have inconsistent data in your database, you could use a LEFT JOIN instead. That way, you'll see all entries from the network_follow table. If there is no such user in the users table, you will still see those entries but the name and surname columns will be NULL.

Upvotes: 1

Francisco Sevilla
Francisco Sevilla

Reputation: 104

You can use the following query instead of use UNION statement:

SELECT t1.*
    ,t2.name
    ,t2.surname
FROM network_follow AS t1
INNER JOIN users AS t2 ON t1.following = t2.username
WHERE t2.following = ':f'

Although you need to think about any ID's columns, Primary and Foreign keys to normalize a bit your tables.

Upvotes: 0

Related Questions