0

希望大家都期待周末。:)

我对一些 php/mysql 有轻微的组织重组问题。我有以下一段运行良好的代码:

// Get the regions our user is assigned to look after
$result1 = mysqli_query($con,"SELECT * FROM regions WHERE staff_100_id='$id'");
while ($row1 = mysqli_fetch_assoc($result1)) {

    // Now get the claims that have come in for the above regions.
    $result2 = mysqli_query($con,"SELECT * FROM registrar_claims WHERE reg_region='$row1[region]' && ready_to_process='yes' ORDER BY claim_id ASC");
    while ($row2 = mysqli_fetch_assoc($result2)) {

        echo $row2[claim_id] ." ";
        echo $row2[reg_1st_name] ." ";
        echo $row2[reg_2nd_name] ."<br>";

        }
    }
}

这个的输出是这样的:

2 Roger Ramjet
7 Snobby Bobgrass
5 Num Nut
6 Phil Pott

我想让输出出来,以便它在整体上由 claim_id 排列,而不仅仅是在 db 调用中的每个周期。所以我希望输出变成:

2 Roger Ramjet
5 Num Nut
6 Phil Pott
7 Snobby Bobgrass

有人愿意向我展示如何重新安排事情以实现这一目标吗?

谢谢,非常感谢!:)

卡斯

4

3 回答 3

0

地区我希望你逃避你的查询条目:)

我要做的是使用子查询压缩查询

SELECT * 
FROM registrar_claims 
WHERE reg_region IN (SELECT region FROM regions WHERE staff_100_id='$id') 
   AND ready_to_process='yes'
ORDER BY claim_id ASC

这应该会产生所需的有序结果

于 2013-11-08T01:19:19.413 回答
0

您可以JOIN使用两个表并在一个查询中完成整个操作。

SELECT *
FROM regions JOIN registrar_claims 
ON registrar_claims.region_id=regions.region 
WHERE regions.staff_100_id='$id' && registrar_claims.ready_to_process='yes' 
ORDER BY registrar_claims.claim_id ASC
于 2013-11-08T01:26:12.153 回答
0

希望这可以帮助:

<?
// Get the regions our user is assigned to look after and all the claims that have come in 
//for the above regions.

$query = 
    "SELECT 
        regions.*,
        registrar_claims.*
    FROM 
        regions inner join registrar_claims on registrar_claims.reg_region = regions.region
    WHERE 
        regions.staff_100_id='$id' and
        registrar_claims.ready_to_process = 'yes'
    ORDER BY
        registrar_claims.claim_id";

while ($row = mysqli_fetch_assoc(mysqli_query($con, $query))) {
        echo $row[claim_id] ." ";
        echo $row[reg_1st_name] ." ";
        echo $row[reg_2nd_name] ."<br>";
}
?>

另外,我建议您查看准备好的语句,以确保代码的安全性和清晰度。

于 2013-11-08T01:35:55.470 回答