希望在总费用超过230之前选择旅游日期和网站名称,并且游览人数超过7人。完整的代码如下,工会的第一部分工作。
SELECT tour_date AS "Departure Date", site_name "Site Name"
FROM partres, reservation, tour, site
WHERE partres.res_id = reservation.res_id
AND reservation.tour_id = tour.tour_id
AND tour.site_id = site.site_id
GROUP BY tour_date, site_name
HAVING COUNT(part_id) > 7
UNION
SELECT tour_date AS "Departure Date", site_name "Site Name"
FROM (
SELECT res_id,tour_date,site_name, (res_partcost +NVL(RES_GEARCOST,0)) as "total_cost"
FROM reservation,site,tour)
WHERE reservation.tour_id = tour.tour_id
AND tour.site_id = site.site_id
AND total_cost > 230
GROUP BY tour_date, site_name;
我仍然有错误
ORA-00904: "TOTAL_COST": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error at Line: 437 Column: 7
由于
答案 0 :(得分:2)
您需要在子查询中移动连接条件
SELECT tour_date AS "Departure Date", site_name "Site Name"
FROM (
SELECT res_id,tour_date,site_name, (res_partcost +NVL(RES_GEARCOST,0)) as "total_cost"
FROM reservation,site,tour
WHERE reservation.tour_id = tour.tour_id
AND tour.site_id = site.site_id ) Res1
WHERE Res1.total_cost > 230 // this will not be displayed in a result
GROUP BY tour_date, site_name;
答案 1 :(得分:0)
union
不会在条件之间给你一个“和”。它会给你一个“或”(因为任何符合任何一个条件的东西都会包括在内)。
我认为你应该将这个短语称为单个查询。此外,正确的连接语法是一个好处:
SELECT tour_date AS "Departure Date", site_name "Site Name"
FROM partres p join
reservation r
on p.res_id = r.res_id join
tour t
on r.tour_id = t.tour_id and
(res_partcost + coalesce(RES_GEARCOST,0)) > 230 join
site s
on t.site_id = s.site_id
GROUP BY tour_date, site_name
having COUNT(part_id) > 7;
这个版本猜测total_cost
在reservation
表中。{/ p>