I have an SQL query that I'm running using WordPress default DB function get_results
like so
$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")
The query is returning wrong results(I think its ignoring the date_time
in WHERE
clause). However, When I'm running this same query manually in phpmyadmin. The same query gives the correct result. I don't know what's happening here.
You should wrap date literal with '
:
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;
Reason:
SELECT 1
WHERE NOW() > '2019-10-07'
-- 0 rows
SELECT 1
WHERE NOW() > 2019-10-07 -- implicit conversion
-- 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.