据我所见,查询确实返回了一个结果,但COALESCE()
结果没有命名,因此MySQL和WordPress使用整个COALESCE()
作为字段名查询。
为了避免这种情况,您可以为COALESCE()
, e、 g。
//$query = "SELECT COALESCE( <your queries> ) AS <alias>";
$query = "SELECT COALESCE( NULL, 5, 2, 3 ) AS order_number";
$results = $wpdb->get_results( $query );
// Assuming $results[0] exists:
echo $results[0]->order_number;
$result = $wpdb->get_row( $query );
// Assuming the query returned a result:
echo $result->order_number;
但是自从
COALESCE()
只返回一个结果,您可以使用
get_var()
:
//$query = "SELECT COALESCE( NULL, 5, 2, 3 )"; // this works
// But maybe, better with an alias.
$query = "SELECT COALESCE( NULL, 5, 2, 3 ) AS order_number";
$result = $wpdb->get_var( $query );
echo $result;