This topic is locked
[SOLVED]

 Trying to use IF statement and stuck now

2/22/2021 10:40:32 AM
PHPRunner General questions
S
swanside author

Hi

Just a quick test I am looking at. I have an invoice generator, but if the field VATChargeable contains YES, then I want the Total field to be the sum of the Subtotal+VATAmount, but if it contains NO, then I want the Total field to be the same as the Subtotal field.

Any advice on how I can make this work please?

Thanks

SELECT

Job_No,

Invoice_Tax_Date,

Contract,

CustomerRef,

Order_Date,

File_No,

VAT,

CustomerName,

CustomerBuildingNumber,

Line1,

Line2,

Line3,

Line4,

Line5,

PostCode,

OrderSiteAddress,

SitesBuildingNumber,

SLine1,

SLine2,

SLine3,

SLine4,

SLine5,

SPostCode,

VATChargeable,

IFNULL((select ROUND(sum(InvoiceVAT),2) from labour where Job_No = job.Job_No), 0) as LabVAT,

IFNULL((select ROUND(sum(InvoiceSubtotal),2) from labour where Job_No = job.Job_No), 0) as Labsubtotal,

IFNULL((select ROUND(sum(Invoice),2) from labour where Job_No = job.Job_No), 0) as LabTotal,

IFNULL((select ROUND(sum(InvoiceVAT),2) from material where Job_No = job.Job_No), 0) as MatVAT,

IFNULL((select ROUND(sum(InvoiceSubtotal),2) from material where Job_No = job.Job_No), 0) as Matsubtotal,

IFNULL((select ROUND(sum(Invoice),2) from material where Job_No = job.Job_No), 0) as MatTotal,

(select Labsubtotal+Matsubtotal) as Subtotal,

(select LabVAT+MatVAT) as VATAmount,

_**IF ((`VATChargeable` = 'YES' SET `Total` == 'Subtotal+VATAmount'; Else SET `Total` == 'Subtotal')),**_

FROM job
Sergey Kornilov admin 2/23/2021

You are trying to use IF syntax that will only work in stored procedures and will not work in a SQL query.
There is a much easier syntax for what you looking for:

SELECT IF(1>3,'true','false')


More example:

https://www.w3resource.com/mysql/control-flow-functions/if-function.php

S
swanside author 2/26/2021



You are trying to use IF syntax that will only work in stored procedures and will not work in a SQL query.
There is a much easier syntax for what you looking for:

SELECT IF(1>3,'true','false')


More example:

https://www.w3resource.com/mysql/control-flow-functions/if-function.php


Cheers

Will take a look at that.
Paul <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=93937&image=1&table=forumreplies' class='bbc_emoticon' alt=':)' />