简体   繁体   中英

SQL Syntax Error in PHP

I keep getting the following error in PHP: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near

'IN (SELECT invoiceNo FROM sales2012 GROUP BY invoiceNo HAVING COUNT(invoiceNo)>1' 
at line 1

My query is as follows:

$query = "SELECT invoiceNo, invoiceDate, dueDate, customerID, customerName, 
dateTrans, postDate, description, grossSale, vat, netSale, quantity, 
branch, despatchNote 
FROM 
sales2012 WHERE invoiceNo IN (
    SELECT invoiceNo FROM sales2012 GROUP BY invoiceNo HAVING COUNT(invoiceNo)>1)";

When I run a simple query like SELECT * FROM sales2012 I get the perfect result and even when I run the query up until the IN clause it runs as well. For some reason it crashes when I run the whole thing.

I also ran the same code using SQL Workbench and it printed out the table that I was looking for. I know the code works, I just can't figure out why it doesn't work in PHP.

I also changed the execution time but that didn't work either.

Any help would be much appreciated. Thanks

the correct query is :

 $query = "SELECT invoiceNo, invoiceDate, dueDate, customerID, customerName, dateTrans, postDate, description, grossSale, vat, netSale, quantity, branch, despatchNote 
           FROM sales2012 
           WHERE invoiceNo IN (SELECT invoiceNo FROM sales2012 GROUP BY invoiceNo HAVING COUNT(invoiceNo)>1)";

but the better query is:

   $query = "SELECT invoiceNo, invoiceDate, dueDate, customerID, customerName, dateTrans, postDate, description, grossSale, vat, netSale, quantity, branch, despatchNote 
           FROM sales2012 s1
           INNER JOIN (SELECT invoiceNo FROM sales2012 GROUP BY invoiceNo HAVING COUNT(invoiceNo)>1) 
           as s2 on s2.invoiceNo = s1.invoiceNO";

If you look at this:

FROM sales2012 IN (SELECT invoiceNo 

You will see that something is missing. It should resemble

FROM sales2012 
where somefield IN (SELECT invoiceNo 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM