所以我有這個查詢(即不是用於生產,只是一個查詢從數據庫中獲得一些數據管理上無論性能)MySQL不能訪問別名的子查詢
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'
線造成的問題是第3子查詢:
(SELECT COUNT(m.id) FROM scheduled_emails m JOIN reservations r ON (r.guest_id = `guests`.`id` AND m.reservation_id = r.id)) "emails",
任何人有,爲什麼在這裏失敗,任何想法?我的意思是我沒有使用別名,guest虛擬機是表的名稱,它在2個第一個子查詢中被讀取成OK。第三個子查詢的區別在於它使用JOIN ...任何想法?謝謝
感謝它的工作! – user28490