【问题标题】:How to use momentJS with nodejs and mysql custom query如何将 momentJS 与 nodejs 和 mysql 自定义查询一起使用
【发布时间】:2021-10-23 18:37:51
【问题描述】:

我要实现如下sql查询:

date_count = await customQuery(
    `
        SELECT 
            COUNT(org.name 
                OR c.name 
                OR co.name 
                OR seg.arrival_details 
                OR seg.departure_details 
                OR seg.segment_airline_id 
                OR seg.segment_arrival_airport_code
                OR seg.segment_departure_airport_code 
                OR seg.segment_flight_number 
                OR seg.trip_id 
                OR b.trip_id
                OR bf.pnr_no 
                OR bf.airline_pnr_no 
                OR b.booking_status 
                OR b.created 
                OR org.organisation_id 
                OR org.name
            ) AS total_count
        FROM 
            segment_details AS seg
            LEFT JOIN trip_details AS tr ON seg.trip_id = tr.trip_id
            LEFT JOIN bookings_flight AS bf ON tr.bookings_flight_id = bf.booking_flight_id
            LEFT JOIN bookings AS b ON bf.booking_id = b.booking_id
            LEFT JOIN organisation AS org ON b.organisation_id = org.organisation_id
            LEFT JOIN city AS c ON org.city_id = c.city_id
            LEFT JOIN country AS co ON org.country_id = co.country_id
        WHERE 
            seg.segment_departure_date BETWEEN 
                ${moment(date.startDate).startOf('day').format("YYYY-MM-DD HH:mm:ss") }
                AND ${moment(date.endDate).endOf('day').format("YYYY-MM-DD HH:mm:ss")}  
    `)

但我收到以下错误:

UnhandledPromiseRejectionWarning: Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '00:00:00

我的日期结果如下: '2021-10-01T08:00:00.000Z'

当我将 moment js 应用到它时,我的结果如下: '2021-10-01 00:00:00'

我正在使用 MySQL,SQL Yog

如何解决此错误?任何帮助或建议将不胜感激。

【问题讨论】:

    标签: mysql sql node.js


    【解决方案1】:

    尝试用单引号括住您的日期:

    ...
    seg.segment_departure_date BETWEEN 
        '${moment(date.startDate).startOf('day').format("YYYY-MM-DD HH:mm:ss")}'
        AND '${moment(date.endDate).endOf('day').format("YYYY-MM-DD HH:mm:ss")}'  
    

    【讨论】:

      【解决方案2】:

      试试看。需要加单引号

      const startDate = moment(date.startDate).startOf('day').format("YYYY-MM-DD HH:mm:ss");
      const endDate = moment(date.endDate).endOf('day').format("YYYY-MM-DD HH:mm:ss");
      
      ...
      
      WHERE 
         seg.segment_departure_date BETWEEN '${startDate}' AND '${endDate}'
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-25
        • 2017-10-11
        • 2018-04-01
        • 2019-06-08
        • 2011-08-18
        • 1970-01-01
        • 1970-01-01
        • 2018-06-02
        相关资源
        最近更新 更多