gpt4 book ai didi

mysql - 无法将列用于子查询 - MySQL 数据库

转载 作者:行者123 更新时间:2023-11-29 04:36:25 26 4
gpt4 key购买 nike

我无法将此“enquiries.enquiry_id”列用于子查询。我也尝试过使用 alias of table 但没有成功错误代码:1054。“where 子句”中的未知列“enquiries.enquiry_id”

我们可以使用其他方法吗?

Tables
------------------------------
Enquiries
------------------------------
enquiry_id | name
1 A
2 B
3 C
4 D
------------------------------
Bookings
------------------------------
booking_id | name |enquiry_id
1 A 1
2 B 2
3 C 2
4 D 3
------------------------------


SELECT
(SELECT
COUNT(*)
FROM
(SELECT
booking_id
FROM
bookings
WHERE
enquiry_id = enquiries.enquiry_id) AS t) AS total
FROM
enquiries
WHERE
enquiry_id = 2

enter image description here

最佳答案

我认为您的查询可以简化为

SELECT COUNT(e.booking_id) as total
FROM enquiries e
WHERE e.enquiry_id = 2

或者如果您需要预订

的其他东西
SELECT COUNT(b.booking_id)  as total
FROM bookings b
JOIN enquiries e
ON b.enquiry_id = e.enquiry_id
WHERE e.enquiry_id = 2

并向您展示如何修复您的查询。

SELECT 
(SELECT COUNT(*)
FROM bookings b
WHERE b.enquiry_id = e.enquiry_id) AS total
FROM
enquiries e
WHERE
enquiry_id = 2

关于mysql - 无法将列用于子查询 - MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40424786/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com