问题
I'm new to PHP and I have spent several hours researching and trying to figure out what I'm doing wrong. I'm joining several tables to populate a client profile page with multiple transactions from a transactions table. The rest of the page is working with the query as expected, but when using a while statement, each transaction in the result is repeating equal to the total number of transactions in the result. For instance, if the result has 3 transaction numbers (1, 2, 3), the while statement is echoing "1, 1, 1, 2, 2, 2, 3, 3, 3" instead of just "1, 2, 3".
I know it is something simple, but it has me pulling my hair out. Please help:
if(!isset($_GET['PeopleID'])){
header('Location: people.php');
die();
} else {
if(!filter_var($_GET['PeopleID'], FILTER_VALIDATE_INT)){
header('Location: people.php');
die();
} else {
$PeopleID = filter_var($_GET['PeopleID'], FILTER_VALIDATE_INT);
$query = "SELECT *
FROM
people
LEFT JOIN notes ON
PersonID = PeopleID
LEFT JOIN filenumbers ON
id = PeopleID
LEFT JOIN transactions ON
transactions.FileNumberID = filenumbers.FileNumberID
LEFT JOIN filingdetails ON
filingdetails.FileID = filenumbers.FileNumberID
WHERE PeopleID=:PeopleID";
$result = $db_connection->prepare($query);
$result->execute([
'PeopleID' => $PeopleID
]);
}
}
while($row = $result -> fetch()){
echo $row['TransactionNumber'] . ", ";
}
回答1:
Okay... So, this is expected behaviour from your SQL
query.
When you LEFT JOIN
in the manner that you have you end up getting all of the data multiple times over. This is best explained with an example...
Example
Lets assume you have three tables:
table1
=> your primary table
table2
=> many-to-one relationship with table1
table3
=> many-to-one relationship with table1
table1
id, value
1, a
2, b
3, c
table2
id, fkey, value
1, a, xxxx
2, a, cxcz
3, a, bdtht
4, a, nbtyt
5, b, ngftht
6, b, thhgfj
7, b, gjfhj
8, c, hjghj
9, c, jhgjh
table3
id, fkey, value
10, 2, sdgvgd
11, 2, gjkmhkjk
12, 3, kjhkl
13, 3, kljj
Next we want to SELECT
data from table1
whilst incorporating data from table2
, using a LEFT JOIN
SELECT
table1.id, table1.value, table2.id, table2.value
FROM table1
LEFT JOIN table2 ON
table2.fkey = table1.id
The output of the above SQL
would look something like the below...
table1.id, table1.value, table2.id, table2.value
1, a, 1, xxxx
1, a, 2, cxcz
1, a, 3, bdtht
1, a, 4, nbtyt
2, b, 5, ngftht
2, b, 6, thhgfj
2, b, 7, gjfhj
3, c, 8, hjghj
3, c, 9, jhgjh
Notice that the above output is effectively every selected row in table1
multiplied by the number of corresponding rows in table2
; OR...
every row in table1 * table2 rows with fkey=table1.id
So, that's simple enough. What if we now look at an example with the three tables and two LEFT JOINs
SELECT
table1.id, table1.value, table2.id, table2.value
FROM table1
LEFT JOIN table2 ON
table2.fkey = table1.id
LEFT JOIN table3 ON
table3.fkey = table1.id
table1.id, table1.value, table2.id, table2.value, table3.id, table3.value
1, a, 1, xxxx, null, null
1, a, 2, cxcz, null, null
1, a, 3, bdtht, null, null
1, a, 4, nbtyt, null, null
2, b, 5, ngftht, 10, 2, sdgvgd
2, b, 5, ngftht, 11, 2, gjkmhkjk
2, b, 6, thhgfj, 10, 2, sdgvgd
2, b, 6, thhgfj, 11, 2, gjkmhkjk
2, b, 7, gjfhj, 10, 2, sdgvgd
2, b, 7, gjfhj, 11, 2, gjkmhkjk
3, c, 8, hjghj, 12, 3, kjhkl
3, c, 8, hjghj, 13, 3, kljj,
3, c, 9, jhgjh, 12, 3, kjhkl
3, c, 9, jhgjh, 13, 3, kljj
Here you can see that we now have output equivalent to:
every row in table1 * table2 rows with fkey=table1.id * table3 rows with fkey=table1.id
来源:https://stackoverflow.com/questions/64516874/php-while-statement-echoes-duplicates