[英]MySQL Join where matched columns contain different values
The below Select used to work perfectly using Mysql_Select with a for loop however since updating to mysqli_connect and using a while loop I've hit a snag. 下面的Select过去常与带有for循环的Mysql_Select配合使用,但是由于更新到mysqli_connect并使用while循环,我遇到了麻烦。
$query =
"SELECT *
FROM
jobs
LEFT JOIN invoices ON jobs.jobID=invoices.jobID
WHERE jobs.customerID = '$selectedcustID'
ORDER BY date_auto DESC";
$result = mysqli_query($connection, $query) or die(mysqli_error());
if(mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$selectedjobID = $row["jobID"];
$date = $row["date_auto"];
$formatdate = date("d/m/Y", $date);
$status = $row["status"];
$notes = $row["notes"];
$invoiceStatus = $row["invoiceStatus"];
$invoicePaid = $row["paid"];
$invoiceNumber = $row["invoiceID"];
$formatinvoiceNumber = sprintf("%05d", $invoiceNumber);
The common parameter is the jobID
column in the jobs
and invoices
tables but, as the invoice will not exist until raised, the display of the jobID
in my php output list of jobs is now blank. 常见的参数是
jobID
列中的jobs
和invoices
表,但,因为发票不会存在,直到提出的的显示jobID
的作业我的PHP输出列表是当前空白。 I think the reason is that it is using the invoices version of jobID (being NULL
) rather than the jobs version. 我认为原因是它使用的是jobID的发票版本(为
NULL
),而不是作业版本。
I've tried all kinds of ways of working around this but am running round in circles. 我尝试了各种方法来解决此问题,但是却绕圈而行。 I'm thinking that it will be a case of saying SELECT * from jobs and SELECT jobID AS but I'm not sure on the correct syntax.
我认为这将是从作业中选择SELECT *和SELECT jobID AS的一种情况,但我不确定语法是否正确。
Any help would be greatly appreciated. 任何帮助将不胜感激。
As the invoice will not exist until raised, the display of the jobID in my php output list of jobs is now blank
由于发票直到提出才存在,所以我的php输出作业列表中的jobID显示为空白
Don't use SELECT *
: you have a jobID
field on both ends of the LEFT JOIN
, and you end up with the wrong field being picked (ie invoices.jobID
, which is NULL
when the invoice was not yet raised). 不要使用
SELECT *
:您在LEFT JOIN
两端都有一个jobID
字段,并且最终选择了错误的字段(即invoices.jobID
,当尚未提出发票时为NULL
)。
You want to explicitely select the fields you need, like : 您要明确选择所需的字段,例如:
SELECT
jobs.jobID,
...
FROM
jobs
LEFT JOIN invoices ON jobs.jobID = invoices.jobID
WHERE jobs.customerID = '$selectedcustID'
ORDER BY date_auto DESC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.