所以我有这个查询(不是用于生产用途,只是一个查询以管理从数据库中获取一些数据而不管性能如何)
SELECT
`guests`.`id`,
CONCAT(u.`fname`, '', u.`lname`) "name",
`u`.`id` "u",
(SELECT COUNT(r.id) FROM reservations r WHERE r.guest_id=guests.id) "reservations",
(SELECT COUNT(t.id) FROM tasks t WHERE t.guest_id = guests.id) "tasks",
(SELECT COUNT(m.id) FROM scheduled_emails m JOIN reservations r ON (r.guest_id = `guests`.`id` AND m.reservation_id = r.id)) "emails",
`guests`.`created` "created",
`guests`.`updated` "updated"
FROM `guests`
JOIN `users` AS u ON u.`id` = `guests`.`user_id` LIMIT 0,25
我得到
#1054 - Unknown column 'guests.id' in 'on clause'
导致问题的行是第三个子查询:
(SELECT COUNT(m.id) FROM scheduled_emails m JOIN reservations r ON (r.guest_id = `guests`.`id` AND m.reservation_id = r.id)) "emails",
有人知道为什么它在这里失败了吗?我的意思是我没有使用别名,guests 是表的名称,它在 2 个第一个子查询中读取正常。第三个子查询的不同之处在于它使用的是 JOIN ......有什么想法吗?谢谢