MYSQL if else查询语句

时间:2018-01-29 02:55:43

标签: mysql vb.net

我的查询有问题。请帮助我。

IF (invoicesearch.CLIENTSNAME <> '' AND invoicesearch.FROM <> '' AND invoicesearch.TO <> '' AND invoicesearch.MIN <> '' AND invoicesearch.MAX <> '' AND invoicesearch.STATUS <> '') then 

SELECT invoicetable.INVOICENUMBER, invoicetable.INVOICEDATE, invoicetable.DUEDATE, invoicetable.CLIENTSNAME, invoicetable.TOTAL,  invoicetable.`STATUS`, Sum(replace(invoicetable.TOTAL,',','')) 
FROM invoicetable , invoicesearch 
WHERE INVOICETABLE.CLIENTSNAME = invoicesearch.clientname 
AND invoicetable.FROM =  invoicesearch.FROM 
AND INVOICETABLE.TO = invoicesearch.TO 
AND MIN(replace(invoicetable.TOTAL,',','')) = invoicesearch.MIN 
AND MAX(replace(invoicetable.TOTAL,',','')) = invoicesearch.MAX 
AND invoicetable.STATUS = invoicesearch.status 
GROUP BY invoicetable.INVOICENUMBER, invoicetable.INVOICEDATE, invoicetable.DUEDATE, invoicetable.CLIENTSNAME, invoicetable.TOTAL,  invoicetable.`STATUS`

1 个答案:

答案 0 :(得分:0)

对于您使用IF指定的条件,只需将其添加到WHERE子句中即可。请随意尝试此查询:

SELECT 
    invoicetable.INVOICENUMBER, 
    invoicetable.INVOICEDATE, 
    invoicetable.DUEDATE, 
    invoicetable.CLIENTSNAME, 
    invoicetable.TOTAL,  
    invoicetable.`STATUS`, 
    Sum(replace(invoicetable.TOTAL, ',' ,''))

FROM 
    invoicetable, 
    invoicesearch

WHERE 
    INVOICETABLE.CLIENTSNAME = invoicesearch.clientname 
    AND invoicetable.FROM =  invoicesearch.FROM 
    AND INVOICETABLE.TO = invoicesearch.TO 
    AND MIN(replace(invoicetable.TOTAL,',','')) = invoicesearch.MIN 
    AND MAX(replace(invoicetable.TOTAL,',','')) = invoicesearch.MAX 
    AND invoicetable.STATUS = invoicesearch.status 
    AND invoicesearch.CLIENTSNAME <> '' 
    AND invoicesearch.FROM <> '' 
    AND invoicesearch.TO <> '' 
    AND invoicesearch.MIN <> '' 
    AND invoicesearch.MAX <> '' 
    AND invoicesearch.STATUS <> ''

GROUP BY 
    invoicetable.INVOICENUMBER, 
    invoicetable.INVOICEDATE, 
    invoicetable.DUEDATE, 
    invoicetable.CLIENTSNAME, 
    invoicetable.TOTAL,  
    invoicetable.`STATUS`;