2

我目前正在尝试运行 SQL 查询以在某个日期之间导出数据,但它可以正常运行查询,只是不是日期选择,我无法弄清楚出了什么问题。

SELECT 
    title AS Order_No, 
    FROM_UNIXTIME(entry_date, '%d-%m-%Y') AS Date, 
    status AS Status,
    field_id_59 AS Transaction_ID,  
    field_id_32 AS Customer_Name, 
    field_id_26 AS Sub_Total, 
    field_id_28 AS VAT, 
    field_id_31 AS Discount, 
    field_id_27 AS Shipping_Cost, 
    (field_id_26+field_id_28+field_id_27-field_id_31) AS Total 
FROM 
    exp_channel_data AS d NATURAL JOIN
    exp_channel_titles AS t
WHERE 
    t.channel_id = 5 AND FROM_UNIXTIME(entry_date, '%d-%m-%Y') BETWEEN '01-05-2012' AND '31-05-2012' AND status = 'Shipped'  
ORDER BY 
    entry_date DESC
4

2 回答 2

6

手册中所述,日期文字应采用YYYY-MM-DD格式。另外,请记住@ypercube 在他的回答中提出的观点,您想要:

WHERE t.channel_id = 5
  AND entry_date >= UNIX_TIMESTAMP('2012-05-01')
  AND entry_date <  UNIX_TIMESTAMP('2012-06-01')
  AND status = 'Shipped'
于 2012-05-18T12:22:45.197 回答
3

除了日期格式还有另一个问题。为了有效地使用 上的任何索引,当您在, or子句entry_date中使用条件时,不应将函数应用于该列(如果您需要与默认格式不同的显示格式,则可以使用列表中的格式)。编写该部分查询的有效方法是:WHEREGROUP BYHAVINGSELECT

 (  entry_date >= '2012-05-01'  
AND entry_date < '2012-06-01' 
 )

It works with DATE, DATETIME and TIMESTAMP columns.

于 2012-05-18T12:29:59.940 回答