wpdb COALESCE won’t work
Question
I am struggling to get the result using COALESCE
from custom table. I need to run a second query if the first results null but wpdb
resulting query string itself rather than the result.
$query = "SELECT coalesce(";
$query .= "(SELECT order_number FROM wp_gs_p_cart ";
$query .= "WHERE group_user = %d AND group_id = %d AND identifier = %s ORDER BY id DESC LIMIT 1),";
$query .= "(SELECT order_number FROM wp_gs_p_cart ORDER BY id DESC LIMIT 1)";
$query .= ");";
$prepare = $wpdb->prepare($query, $user_id, $group_id, $identifier);
return $wpdb->get_results($prepare);
// or
return $wpdb->get_row($prepare);
Result
Array
(
[0] => stdClass Object
(
[coalesce((SELECT order_number FROM wp_gs_p_cart WHERE group_user = 73 AND group_id = 298 AND identifier = 'AtR4deAVgU4Ensi1_1ac2b33a4df7c5f0cf148d6232074352' ORDER BY id DESC LIMIT 1),(SELECT order_number FROM wp_gs_p_cart ORDER BY id DESC LIMIT 1))] => 3333
)
)
0
2 months
0 Answers
16 views
0
Leave an answer
You must login or register to add a new answer .