我有一个使用WordPress默认数据库功能get_results
运行的SQL查询
像这样
$result = $wpdb->get_results("SELECT *, 'cash' AS mop
FROM wp_wpsp_cash_transactions
WHERE 1=1 AND DATE(date_time) > 2018-10-07 UNION ALL
SELECT *, 'bank' AS mop FROM wp_wpsp_bank_transactions
WHERE 1=1 AND DATE(date_time) > 2018-10-07 ORDER BY date_time DESC")
查询返回错误结果(我认为它忽略了date_time
子句中的WHERE
)。但是,当我在phpmyadmin中手动运行此查询时。相同的查询给出正确的结果。我不知道这里发生了什么。
答案 0 :(得分:2)
您应使用'
包装日期文字:
SELECT *, 'cash' AS mop
FROM wp_wpsp_cash_transactions
WHERE 1=1 AND DATE(date_time) > '2018-10-07' -- comparing with date not int
UNION ALL
SELECT *, 'bank' AS mop
FROM wp_wpsp_bank_transactions
WHERE 1=1 AND DATE(date_time) > '2018-10-07'
ORDER BY date_time DESC;
原因:
SELECT 1
WHERE NOW() > '2019-10-07'
-- 0 rows
SELECT 1
WHERE NOW() > 2019-10-07 -- implicit conversion
-- 1