我的Sql查询
SELECT BOOKING_TIME,
Contact_No,
(FName+LName)AS NAME ,
E_MAIL,
(SELECT ZM.ZONE_NAME
FROM Zone_Master ZM
INNER JOIN BOOKINGS ON ZM.Zone_ID = BOOKINGS.Zone_ID)AS ZONE_NAME,
City,
Addr_1,
Addr_2,
PIN,
(SELECT PROJECTS.PROJECT_NAME
FROM PROJECTS
INNER JOIN BOOKINGS ON PROJECTS.PROJECT_ID=BOOKINGS.PROJECT_ID)AS PROJECT_NAME
FROM BOOKINGS
答案 0 :(得分:4)
由于您的子查询,您收到了该错误:
(SELECT ZM.ZONE_NAME
FROM Zone_Master ZM
INNER JOIN BOOKINGS ON ZM.Zone_ID = BOOKINGS.Zone_ID) AS ZONE_NAME
和
(SELECT PROJECTS.PROJECT_NAME
FROM PROJECTS
INNER JOIN BOOKINGS ON PROJECTS.PROJECT_ID = BOOKINGS.PROJECT_ID) AS PROJECT_NAME
您将获得多条记录并尝试将它们存储在一个字段中。
答案 1 :(得分:3)
以下是您的查询:
SELECT BOOKING_TIME, Contact_No,(FName+LName)AS NAME, E_MAIL,
(SELECT ZM.ZONE_NAME
FROM Zone_Master ZM INNER JOIN
BOOKINGS
ON ZM.Zone_ID = BOOKINGS.Zone_ID
) AS ZONE_NAME,
City, Addr_1, Addr_2, PIN,
(SELECT PROJECTS.PROJECT_NAME
FROM PROJECTS INNER JOIN
BOOKINGS
ON PROJECTS.PROJECT_ID=BOOKINGS.PROJECT_ID
) AS PROJECT_NAME
FROM BOOKINGS;
子查询可能返回多行。在select
子句的子选择中,您只能返回一个值。我认为有一个简单的解决方法。您可能需要相关的子查询,因此只需从每个子查询中删除BOOKINGS
表:
SELECT BOOKING_TIME, Contact_No, (FName+LName)AS NAME, E_MAIL,
(SELECT ZM.ZONE_NAME
FROM Zone_Master ZM
WHERE ZM.Zone_ID = BOOKINGS.Zone_ID
) AS ZONE_NAME,
City,Addr_1,Addr_2,PIN,
(SELECT PROJECTS.PROJECT_NAME
FROM PROJECTS
WHERE PROJECTS.PROJECT_ID = BOOKINGS.PROJECT_ID
)AS PROJECT_NAME
FROM BOOKINGS;
这些现在是“相关子查询”。在这种情况下,他们每个应该最多返回一行。
表达此查询的另一种方法是使用join
语法:
SELECT BOOKING_TIME, Contact_No, (FName+LName)AS NAME, E_MAIL,
ZM.ZONE_NAME,
City, Addr_1, Addr_2, PIN,
p.PROJECT_NAME
FROM BOOKINGS b LEFT OUTER JOIN
Zone_Master zm
on ZM.Zone_ID = BOOKINGS.Zone_ID LEFT OUTER JOIN
PROJECTS p
on p.PROJECT_ID = b.PROJECT_ID