Link to home
Start Free TrialLog in
Avatar of lawrence_dev
lawrence_dev

asked on

Need assistance with join statement PHP/PDO

How do I correctly structure this join statement?

$query1 = $conn->query("SELECT * FROM `Outdoor_Invoices` LEFT JOIN `oc_order` USING (order_id) WHERE PONo = 'order_id'");

//LEFT JOIN `oc_url_alias` USING (query) WHERE product_id');

    while ($row1 = $query1->fetch(PDO::FETCH_ASSOC))
    {
		
	$orderid = $row1['order_id'];
	$InvAmt = $row1['InvAmt'];
	$OrderTotal = $row1['total'];
	$Profit = ($total - $InvAmt);
	
$conn->query("UPDATE `Outdoor_Invoices` SET OrderTotal = $OrderTotal, Profit = $Profit WHERE PONo = '$orderid'");
						
}

Open in new window

Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

Which one?
Avatar of lawrence_dev
lawrence_dev

ASKER

Thanks Terry!  I tried a little variation and still cannot get it to work.


$query1 = $conn->query("SELECT * FROM `Outdoor_Invoices` LEFT JOIN `oc_order` USING (order_id) WHERE Outdoor_Invoices.PONo = oc_order.'order_id'");
I wonder if it's because order_id is in single quotes rather than backticks? You don't need them anyway for the given table and column names.

Try this:
$query1 = $conn->query("
SELECT * FROM Outdoor_Invoices
LEFT JOIN oc_order USING (order_id) 
WHERE Outdoor_Invoices.PONo = oc_order.order_id
");

Open in new window

Terry,  the columns and table names are correct, however, I am getting the following error:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'order_id' in 'from clause'' in OutdoorProfit.php:125 Stack trace: #0 OutdoorProfit.php(125): PDO->query('SELECT * FROM O...') #1 {main} thrown in OutdoorProfit.php on line 125

Column  /   Table

PONo   /  Outdoor_Invoices

order_id  /  oc_order
ASKER CERTIFIED SOLUTION
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry Terry for the delay!!  That worked great!!  THANKS!!!