Jump to content

Subselect as condition


imperium2335

Recommended Posts

Hi,

 

How would I use a subselect in a WHERE clause? e.g.

 

WHERE pos.jobRef = invoices_out.jobRef
				  AND enquiries.id = pos.enqRef
				  AND DATE(invoices_out.invoiceDate) = CURDATE()
				  AND (SELECT MAX(dateCreated) FROM pos) = invoices_out.jobRef

 

Because I just want it to base its selection on the latest date result.

Link to comment
Share on other sites

I've tried doing this to test how to do it but I get an error saying invalid use of group function :( :

 

 

require("system/dbconnectlocal.php") ;

 

$result = mysql_query("SELECT if(currency = '€', (SELECT amount FROM pos WHERE MAX(dateCreated) AND jobRef = '885'), 0) AS euroTotal FROM pos WHERE jobRef = '885'")or die(mysql_error()) ;

 

while($row = mysql_fetch_assoc($result)) {

 

echo $row['euroTotal'] ;

 

}

Link to comment
Share on other sites

Thanks, that works for my test, but how would I get that into something where its on a per record bases like :

 


$result = mysql_query("SELECT DATE(invoices_out.invoiceDate) AS date,
				  invoices_out.enqRef AS eId,
				  invoices_out.jobRef,
				  invoices_out.branch,
				  invoices_out.customer, 
				  invoices_out.currency, 
				  invoices_out.absoluteTotal, 
				  invoices_out.euroVpound, 
				  invoices_out.dollarVpound,
				  sum(if(pos.currency = '$', pos.amount, 0)) AS dollarTotal,
				  sum(if(pos.currency = '€', pos.amount, 0)) AS euroTotal,
				  sum(if(pos.currency = '£', pos.amount, 0)) AS poundTotal,
				  sum(if(pos.currency = '$', pos.amount/pos.dollarRate, 0))+sum(if(pos.currency = '€', pos.amount/pos.euroRate, 0))+sum(if(pos.currency = '£', pos.amount, 0)) AS totalCosts,
				  enquiries.theirShippingTotal,
				  enquiries.theirShippingCurrency
				  FROM invoices_out, pos, enquiries
				  WHERE pos.jobRef = invoices_out.jobRef
				  AND enquiries.id = pos.enqRef
				  #AND pos.weReceivedInvoice = 1
				  AND DATE(invoices_out.invoiceDate) = CURDATE()
				  GROUP BY pos.enqRef
				  ORDER BY invoices_out.branch")or die(mysql_error()) ;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.