考慮下面的數據庫設置,以獲得更多信息:的一種方式圍繞具有遍歷結果使用第一個結果集
Customers
| customerId | accountId | addressId |
|------------|-----------|-----------|
| 1 | 110 | 8 |
| 2 | 120 | 9 |
| 3 | 130 | 10 |
Address
| addressId | companyName | state |
|-----------|-------------|-------|
| 8 | FooBar Co | FL |
| 9 | Self Co | VA |
| 10 | Cli Co | CA |
Tests
| testId | accountId | testType | Status |
|--------|-----------|----------|---------|
| 1 | 120 | Urine | Done |
| 2 | 110 | Blood | Pending |
| 3 | 110 | Blood | Pending |
| 4 | 130 | Biopsy | Done |
| 5 | 130 | Blood | Done |
| 6 | 130 | Urine | Pending |
| 7 | 110 | Biopsy | Pending |
| 8 | 120 | Urine | Pending |
| 9 | 110 | Biopsy | Pending |
| 10 | 110 | Urine | Pending |
有我繞過通過mysqli的結果集具有循環的方式的客戶,以便根據測試類型的名稱並基於accountId獲取每個測試類型的COUNT。因此,例如我目前做這個:
$sql = "SELECT C.accountId, A.companyName
FROM Customers C
JOIN Address A
ON C.addressId=A.addressId";
$result = mysqli_query($connection, $sql);
$customers = mysqli_fetch_all($result, MYSQLI_ASSOC);
foreach($customers as $customer) :
echo '<h2>' . $customer["companyName"] . '</h2>';
// Urine Tests Count
$sql = 'SELECT COUNT(testId) as count FROM Tests WHERE accountId=' . $customer["accountId"] . ' AND testType="Urine"';
$result = mysqli_query($connection, $sql);
$testCount = mysqli_fetch_array($result, MYSQLI_ASSOC);
echo '<p>Total Urine Tests: ' . $testCount["count"] . '</p>';
// Biopsy Tests Count
$sql = 'SELECT COUNT(testId) as count FROM Tests WHERE accountId=' . $customer["accountId"] . ' AND testType="Biopsy"';
$result = mysqli_query($connection, $sql);
$testCount = mysqli_fetch_array($result, MYSQLI_ASSOC);
echo '<p>Total Biopsy Tests: ' . $testCount["count"] . '</p>';
// Blood Tests Count
$sql = 'SELECT COUNT(testId) as count FROM Tests WHERE accountId=' . $customer["accountId"] . ' AND testType="Blood"';
$result = mysqli_query($connection, $sql);
$testCount = mysqli_fetch_array($result, MYSQLI_ASSOC);
echo '<p>Total Blood Tests: ' . $testCount["count"] . '</p>';
endforeach;
正如你可能會說這是海拉但卻難免重複,但到目前爲止,它已經得到我想要的信息的唯一途徑。我知道我可以做一個COUNT()
與GROUP BY
爲測試一旦我在foreach循環內,但如果我這樣做,我不會回0測試,客戶可能沒有,我需要顯示,如果testType有0.
我很熟悉PhP,然後我在MySQLi和SQL中。有什麼我在這裏失蹤?當然,必須有更好的方法來獲得所有這些,甚至可以在一個查詢中得到它?任何建議或正確方向的一點都會很棒。
如果我得到錯誤列'測試類型'在字段列表中含糊不清'什麼可能導致這一點。 –
對不起,在交叉連接後需要別名Z.TestType(在select和group by中)。 – xQbert
沒問題,我是否需要添加'COUNT(不同的T.testId)'以及否? –