我有兩個表customer
和customer_transaction
。兩者都有共同的customer_id
,但customer_transaction
也有一個字段描述,我想返回我的結果,基於共同的customer_id。 結果正常工作,但省略了所有customer_id和customer_transaction的所有記錄的customer_id。描述按照圖像。我相信我在這裏錯過了一些小東西。PHP MYSQL SELECT和LEFT JOIN - 沒有返回所有結果
相關的代碼的一部分。我似乎認爲問題在於SELECT語句。
$sql = "SELECT * FROM customer LEFT OUTER JOIN customer_transaction ON customer. customer_id =customer_transaction. customer_id WHERE customer.customer_group_id = $input";
$sth = $pdo->prepare($sql);
$sth->bindParam(':start',$start,PDO::PARAM_INT);
$sth->bindParam(':length',$length,PDO::PARAM_INT);
$sth->bindParam(':query',$query,PDO::PARAM_STR);
$sth->execute();
foreach ($sth->fetchAll(PDO::FETCH_ASSOC) as $row) {
$phpvar = "$row[description] "; //creat variable for descriptio so as to limit the length using substr
echo '<tr>';
echo '<td>'. $row['customer_id'] . '</td>';
echo '<td>'. $row['company_name'] . '</td>';
echo '<td>'. $row['firstname'] ." ". $row['lastname'] .'</td>';
echo '<td>'. $row['email'] . '</td>';
echo '<td>'. $row['telephone'] . '</td>';
echo '<td>'. $row['customer_id'] . '</td>';
echo '<td>'. $row['customer_group_id'] . '</td>';
echo '<td>'. substr($phpvar,0) . '</td>'; //Limit the length of the transactions here
echo '<td width=250>';
無論您如何解決問題,答案都不是這樣。 – Strawberry