Вопрос

I am trying to match columns from different tables, matching them to today's date using SYSDATE. The ouput is actually populating the date column with today's date on all rows instead of just one row that actually has today's date in it.

SELECT building.building_code, 
    building.building_name, 
    room_booking.booking_date
FROM building,
     room_booking,
     dual
WHERE to_date(booking_date) = trunc(sysdate)

OUTPUT IS:

BUILDING_CODE BUILDING_NAME   BOOKING_DATE
------------- --------------- ------------
A             ARCADIA         <17-mar-13>  
B             BELIZE          <17-mar-13>  
C             CAMDEN          <17-mar-13>  
D             DENVER          <17-mar-13>  
E             EXETER          <17-mar-13>  
F             FORT PORTAL     <17-mar-13>  
G             GRAMPIAN        <17-mar-13>  
H             HELVITICA       <17-mar-13>  
I             INVICTA         <17-mar-13>  
J             JULIET          <17-mar-13>  

10 rows selected 

There should be only one row in the result as there is only 1 row with today's date in it. Where have I gone wrong?

Это было полезно?

Решение

This is because you're not joining the tables room_booking and building, producing a cartesian product on it.

SELECT 
    b.building_code, 
    b.building_name, 
    r.booking_date
FROM building b
        JOIN room_booking   r /* missing join condition here: ON r.building_code = b.building_code */
WHERE 
    TO_DATE(r.booking_date) = TRUNC(SYSDATE)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top