如何在php中合并两个mysql结果?
问题描述:
As this question clearly means that, I am not asking about SQL
joins here,
I just want to combine/merge 2 MySQL
result in PHP
, i have tried to do it with PHP array_merge() but no success.
$user_paid_query = "SELECT * from users WHERE now_paid!=0 ORDER BY now_paid DESC";
$result_user_paid = $connect->query($user_paid_query);
$users_paid = $result_user_paid->fetch_assoc();
$users_unpaid_query = "SELECT * from users WHERE now_paid=0 ORDER BY id ASC";
$users = array_merge($users_paid, $users_unpaid);
答
You can use union
in MySQL itself(instead of PHP) to merge 2 SQL results.
(SELECT * from users WHERE now_paid != 0 ORDER BY now_paid DESC)
UNION
(SELECT * from users WHERE now_paid = 0 ORDER BY id ASC)