简体   繁体   English

PDO联合选择结果无

[英]PDO union select results nothing

I need to select name , surname from users where username = column following inside network_follow table 我需要选择namesurnameusers那里username =列followingnetwork_follow

the query: 查询:

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

php code: php代码:

 $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 对我来说$fvalue就好了,但是$fname不好

for some reasons union select is not working, can somebody help? 由于某些原因, 工会选择不起作用,有人可以帮忙吗?

A UNION is used to select similar data from multiple tables. UNION用于从多个表中选择相似的数据。

A JOIN is used to combine related data from multiple tables. JOIN用于合并来自多个表的相关数据。

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. 在您的情况下,您想从network_following表中获取信息, 并将其users表中的用户名结合起来 A JOIN is more appropriate here: JOIN在这里更合适:

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. 我在这里使用INNER JOIN来确保您在两个表中看到具有结果的条目。

If you want to see if you have inconsistent data in your database, you could use a LEFT JOIN instead. 如果要查看数据库中的数据是否不一致,可以改用LEFT JOIN That way, you'll see all entries from the network_follow table. 这样,您将看到network_follow表中的所有条目。 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 . 如果在users表中没有这样的users ,您仍然会看到这些条目,但namesurname列将为NULL

You can use the following query instead of use UNION statement: 您可以使用以下查询代替使用UNION语句:

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. 尽管您需要考虑任何ID的列,但可以使用主键和外键对表进行规范化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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