简体   繁体   English

如何在 PDO 中使用 Union

[英]How to use Union in PDO

I looked at the similar questions and answers but not seemed to help.我查看了类似的问题和答案,但似乎没有帮助。

I have a query:我有一个查询:

$stmt = $conn->prepare( 
'SELECT kpp_id FROM kpp_profile WHERE email = :email AND password = :password
UNION ALL
SELECT pm_id FROM pm_profile WHERE email = :email AND password = :password 
UNION ALL
SELECT trainer_id FROM trainer_profile WHERE email = :email AND password = :password');
        $stmt->execute( array( ':email' => $email, ':password' => $password ) );
        $result = $stmt->fetchAll();

For some reason this only returns results for the first query.由于某种原因,这只返回第一个查询的结果。 Can anyone help me understand why?谁能帮我理解为什么?

Are you sure you have data in pm_profile and trainer_profile tables for same supplied email and password?您确定在 pm_profile 和 trainer_profile 表中有相同提供的 email 和密码的数据吗? What happens if you run same sql directly using hard coded same email/password?如果您使用硬编码的相同电子邮件/密码直接运行相同的 sql 会发生什么?

Thank you everyone.谢谢大家。 I realized my mistake.我意识到我的错误。 I was misreading the result set.我误读了结果集。 I did not realize that Union keeps the column name of the first table only.我没有意识到 Union 只保留第一个表的列名。 I was expecting the array to keep the matching tables key value pair.我期待数组保留匹配表的键值对。

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

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