我现在如何显示重复汽车?postgresql

qlvxas9a  于 2023-04-29  发布在  PostgreSQL
关注(0)|答案(1)|浏览(91)

我有一个预订名单,其中两个相同的汽车可以列出,但我只想显示一个,而不是最新的,我想早名单。
我是怎么做到的?

SELECT 

        booking.id,

        cars.name,
        cars.cover as image,

        car_details.mark,

        client.surname,
        client.lastname,
        
        booking_car_status.booking_id as booking_car_status_id
        
        FROM booking

        INNER JOIN cars
        ON booking.car_id = cars.id

        INNER JOIN car_details
        ON car_details.car_id = cars.id

        INNER JOIN client
        ON client.id = booking.client_id
        
        LEFT JOIN booking_car_status
        ON booking_car_status.booking_id = booking.id

        LEFT JOIN booking_signature
        ON booking_signature.booking_id = booking.id

        WHERE (booking_car_status.booking_id IS NULL) AND (booking.from_date::date <= NOW()) ORDER BY booking.id ASC
kqqjbcuj

kqqjbcuj1#

我认为 www.example.com 是表示汽车的独特性和你想拥有的最早的 www.example.com 辆车的com

WITH CTE AS
(
        SELECT 
        booking.id,
        cars.name,
        cars.cover as image,
        car_details.mark,
        client.surname,
        client.lastname,      
        booking_car_status.booking_id as booking_car_status_id ,      
        ROW_NUMBER()OVER(PARTITION BY cars.id ORDER BY booking.id)RN
        
        FROM booking

        INNER JOIN cars
        ON booking.car_id = cars.id

        INNER JOIN car_details
        ON car_details.car_id = cars.id

        INNER JOIN client
        ON client.id = booking.client_id
        
        LEFT JOIN booking_car_status
        ON booking_car_status.booking_id = booking.id

        LEFT JOIN booking_signature
        ON booking_signature.booking_id = booking.id

        WHERE (booking_car_status.booking_id IS NULL) AND (booking.from_date::date <= NOW()) 
)
        SELECT id,
        name,
        image,
        mark,
        surname,
        lastname,      
        booking_car_status_id 
        FROM CTE WHERE RN=1
        ORDER BY id ASC

相关问题