简体   繁体   中英

How to use a select query in the where clause in mysql?

I am trying to retrieve everything from a table where the invoiceNo is distinct.So here is what I tried.

SELECT * FROM  `selected_items` WHERE 'invoiceNo' IN 
(SELECT DISTINCT(invoiceNo) AS invoiceNo FROM selected_items`);

When I try this in php myadmin I get the following warning with no output.

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 '' at line 2.

And when I run my php script from my android app it returns nothing.Is there a place where I can check my query like php code checker.

I am new to programming so any help and suggestion is welcome.Thank you :)

Change this:

SELECT * FROM  `selected_items` WHERE 'invoiceNo' IN 
(SELECT DISTINCT(invoiceNo) AS invoiceNo FROM selected_items`);

To

 SELECT * FROM  `selected_items` WHERE invoiceNo IN (SELECT DISTINCT(invoiceNo) AS invoiceNo FROM selected_items ) GROUP BY invoiceNo

Because invoiceNo is a column name, you should not be using the quotes on it, either leave it like it is or use backticks.

正如Saty在评论中已经提到的那样:您可能的意思是:

SELECT * FROM selected_items GROUP BY invoiceNo;

Remove the "`" symbol end of your code and remove single quotes in field name because values only need single quotes.

Like this:

"SELECT * FROM  `selected_items` WHERE invoiceNo IN 
(SELECT DISTINCT(invoiceNo) AS invoiceNo FROM selected_items);"

Remove the "`" symbol end of your code and

You try this one....

SELECT * FROM `selected_items` WHERE `invoiceNo` IN (SELECT DISTINCT(invoiceNo) AS invoiceNo FROM selected_items);

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