[英]PHP MYSQL SELECT AND LEFT JOIN - not returning all results
I have two tables customer
and customer_transaction
. 我有两个表customer
和customer_transaction
。 Both have common customer_id
, but customer_transaction
also has a field description that I want to return with my result, based on the common customer_id . 两者都有共同的customer_id
,但是customer_transaction
也有一个我想根据共同的customer_id返回结果的字段描述 。 The result works correctly, but omits the customer_id for all records that don't have both customer_id and customer_transaction . 结果工作正常,但省略了不兼得的customer_id和customer_transaction所有记录的CUSTOMER_ID。 description as per image. 根据图片的描述。 I am sure that I am missing something small here. 我确定我在这里缺少一些小东西。
A portion of the relevant code. 相关代码的一部分。 I seem to think the problem lies in the SELECT statement. 我似乎认为问题出在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>';
The problem is that you have customer_id twice. 问题是您有两次customer_id。 The array however can have only one key with the name customer_id
. 但是,该数组只能有一个名称为customer_id
键。 The values are simply copied to the array field by field, using the field name as key. 使用字段名称作为键,将值简单地逐字段复制到数组中。 The value of the second occurrence, the one from the customer_transaction table, is overwriting the one from the customer table, and because not every customer has a transaction, you'll get empty fields there. 第二个出现的值(customer_transaction表中的一个)将覆盖customer表中的一个,并且由于不是每个客户都有一个事务,因此您将在其中获得空字段。
The best solution is to be more exact in the fields you need. 最好的解决方案是在您需要的领域中更加精确。 It is good practise anyway to only fetch the fields you need instead of using *
. 无论如何,最好的做法是仅获取所需的字段,而不要使用*
。
As a result, your query could look like this. 结果,您的查询可能如下所示。 A little more verbose, but with the flexibility of using aliases, calculated values and without the overhead of returning fields that you don't use. 较为冗长,但具有使用别名,计算值的灵活性,并且没有返回不使用的字段的开销。
SELECT
c.customer_id,
c.company_name,
c.firstname,
c.email,
c.telephone,
/* You could provide the field with an alias */
t.customer_id as transaction_customer_id,
/* Or use its value to return a more sensible value */
CASE WHEN t.customer_id IS NULL
THEN 'N'
ELSE 'Y' END AS has_transaction,
t.customer_group_id
FROM customer
LEFT OUTER JOIN customer_transaction ON customer. customer_id = customer_transaction.customer_id
WHERE customer.customer_group_id = $input
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.