如何在PHP中合并两个MySQL结果?

时间:2018-08-02 14:17:37

标签: php mysql

这个问题显然意味着,我并不是在问SQL是否加入这里,

我只想合并/合并2个MySQL结果到PHP中,但是我尝试使用PHP array_merge()来做到这一点,但没有成功。

 $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);

1 个答案:

答案 0 :(得分:2)

您可以在MySQL本身(而不是PHP)中使用union来合并2个SQL结果。

(SELECT * from users WHERE now_paid != 0  ORDER BY now_paid DESC)
UNION
(SELECT * from users WHERE now_paid = 0  ORDER BY id ASC)