将3个查询合并为一个数组

时间:2015-07-22 19:37:12

标签: php mysql arrays

有更好的方法吗?我有3个不同的查询,并希望将它们全部放入一个数组中。由于我无法直接从查询中按照我想要的方式对它们进行排序,因此我必须在3个单独的查询中执行此操作(除非您知道在查询中执行此操作的方法)。

然后我必须创建单独的临时数组才能使foreach循环使用array_push()函数。肯定有比这更好的方法。

$sql = "SELECT prayers.*
    FROM prayers 
    LEFT JOIN prayed_for USING (postid) 
    WHERE prayed_for.username = '" . $_SESSION['user']['username'] . "' 
    and prayers.type = 'prayer' 
    and prayers.privacy != 'hidden'
    and 
    (
        (priority = 'emergency' and date > DATE_SUB(NOW(), INTERVAL 20 DAY)) 
    or 
        (priority = 'urgent' and date > DATE_SUB(NOW(), INTERVAL 40 DAY))
    ) 
    ORDER BY priority DESC, prayers.date DESC";

    $sql2 = "SELECT prayers.*
    FROM prayers 
    LEFT JOIN prayed_for USING (postid) 
    WHERE prayed_for.username = '" . $_SESSION['user']['username'] . "' 
    and prayers.type = 'prayer' 
    and prayers.privacy != 'hidden'
    and priority = 'normal' 
    and date > DATE_SUB(NOW(), INTERVAL 60 DAY)
    ORDER BY prayers.date DESC";

    $sql3 = "SELECT prayers.*
    FROM prayers 
    LEFT JOIN prayed_for USING (postid) 
    WHERE prayed_for.username = '" . $_SESSION['user']['username'] . "' 
    and prayers.type = 'prayer' 
    and prayers.privacy != 'hidden'
    and priority = 'long_term' 
    ORDER BY prayers.date DESC";

    $_SESSION['aprayers'] = send_query($sql);
    $rows2 = array();
    $rows2 = send_query($sql2);
    foreach ($rows2 as $row) {
        array_push($_SESSION['aprayers'],$row);
    }
    $rows3 = array();
    $rows3 = send_query($sql3);
    foreach ($rows3 as $row) {
        array_push($_SESSION['aprayers'],$row);
    }

4 个答案:

答案 0 :(得分:3)

我相信您应该能够在不使用UNION的情况下在一个查询中执行此操作,方法是组合您的条件并使用CASE对优先级进行排序。我真的不确定它的性能如何与UNION解决方案相比,但它消除了代码中的一些冗余,因为它的价值。

$sql = "SELECT prayers.*
    FROM prayers 
    LEFT JOIN prayed_for USING (postid) 
    WHERE prayed_for.username = '" . $_SESSION['user']['username'] . "' 
    and prayers.type = 'prayer' 
    and prayers.privacy != 'hidden'
    and 
    (
        (priority = 'emergency' and date > DATE_SUB(NOW(), INTERVAL 20 DAY)) 
    or 
        (priority = 'urgent' and date > DATE_SUB(NOW(), INTERVAL 40 DAY))
    or
        (priority = 'normal' and date > DATE_SUB(NOW(), INTERVAL 60 DAY))
    or
        priority = 'long_term'     
    ) 
    ORDER BY 
    CASE priority 
        WHEN 'emergency' THEN 1 
        WHEN 'urgent' THEN 2
        WHEN 'normal' THEN 3 
        WHEN 'long_term' THEN 4
    END, prayers.date DESC";

答案 1 :(得分:1)

$sql_queries = array($sql1, $sql2, $sql3);
$_SESSION['aprayers'] = array();
foreach($sql_queries as $sql_query)
{
  $_SESSION['aprayers'] = array_merge($_SESSION['aprayers'], send_query($sql_query));
}

答案 2 :(得分:1)

您可以使用UNION语法将它们合并为一个。

https://dev.mysql.com/doc/refman/5.0/en/union.html

示例(更新):

(SELECT prayers.*, 1 as order
  FROM prayers AS p
  LEFT JOIN prayed_for AS f USING (postid) 
  WHERE prayed_for.username = '" . $_SESSION['user']['username'] . "' 
    and prayers.type = 'prayer' 
    and prayers.privacy != 'hidden'
    and 
    (
      (priority = 'emergency' and date > DATE_SUB(NOW(), INTERVAL 20 DAY)) 
    or 
      (priority = 'urgent' and date > DATE_SUB(NOW(), INTERVAL 40 DAY))
    ) 
) UNION (
SELECT prayers.*, 2 as order
  FROM prayers 
  LEFT JOIN prayed_for USING (postid) 
  WHERE prayed_for.username = '" . $_SESSION['user']['username'] . "' 
    and prayers.type = 'prayer' 
    and prayers.privacy != 'hidden'
    and priority = 'normal' 
    and date > DATE_SUB(NOW(), INTERVAL 60 DAY)
) UNION (
SELECT prayers.*, 3 as order
  FROM prayers 
  LEFT JOIN prayed_for USING (postid) 
  WHERE prayed_for.username = '" . $_SESSION['user']['username'] . "' 
    and prayers.type = 'prayer' 
    and prayers.privacy != 'hidden'
    and priority = 'long_term' 
) ORDER BY order, priority, p.date

答案 3 :(得分:1)

以下是对所有人的一个查询:

SELECT prayers.*
FROM prayers 
LEFT JOIN prayed_for USING (postid) 
WHERE prayed_for.username = '" . $_SESSION['user']['username'] . "' 
and prayers.type = 'prayer' 
and prayers.privacy != 'hidden'
and 
(
    (priority = 'emergency' and date > DATE_SUB(NOW(), INTERVAL 20 DAY)) 
or 
    (priority = 'urgent' and date > DATE_SUB(NOW(), INTERVAL 40 DAY))
) 
ORDER BY priority DESC, prayers.date DESC

union all

SELECT prayers.*
FROM prayers 
LEFT JOIN prayed_for USING (postid) 
WHERE prayed_for.username = '" . $_SESSION['user']['username'] . "' 
and prayers.type = 'prayer' 
and prayers.privacy != 'hidden'
and priority = 'normal' 
and date > DATE_SUB(NOW(), INTERVAL 60 DAY)
ORDER BY prayers.date DESC

union all

SELECT prayers.*
FROM prayers 
LEFT JOIN prayed_for USING (postid) 
WHERE prayed_for.username = '" . $_SESSION['user']['username'] . "' 
and prayers.type = 'prayer' 
and prayers.privacy != 'hidden'
and priority = 'long_term' 
ORDER BY prayers.date DESC