我有一个完全符合预期的联接,除了从'right'表中选择的任何和所有字段在肯定不是时返回空白。
SELECT score.recipient, score.amount, u.* FROM score
LEFT JOIN `users` AS u ON score.recipient = u.id AND u.team_id = ?
WHERE UNIX_TIMESTAMP(score.date) > ?
我实际上并不需要整个用户表,只需要users.email - 但没有字段可用。结果集看起来像这样(样本):
[0] => stdClass Object ( [recipient] => 1 [amount] => 1 [id] => [fname] => [lname] => [nickname] => [email] => [phone] => [reg_key] => )
[1] => stdClass Object ( [recipient] => 103 [amount] => -1 [id] => [fname] => [lname] => [nickname] => [email] => [phone] => [reg_key] => )
列出的所有字段实际上已填充。
任何帮助将不胜感激!我很茫然。
答案 0 :(得分:2)
如果用内连接替换左连接返回空结果集,则连接条件/ where子句会被破坏。
尝试这个(没有绑定变量及其条件)并查看它是否返回任何值:
SELECT score.recipient, score.amount, u.* FROM score
LEFT JOIN `users` AS u ON score.recipient = u.id
如果是这种情况,那么看看你得到的team_id / score.date的值 - 我敢打赌你正在使用表格中根本不存在的绑定值的组合。