如何在mysql的where子句中使用select查询?

如何在mysql的where子句中使用select查询?

问题描述:

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 :)

我正在尝试从invoiceNo不同的表中检索所有内容。所以这就是我尝试过的。 p>

  SELECT * FROM`selected_items` WHERE'ynoryNo'IN 
(SELECT DISTINCT(invoiceNo)AS invoiceNo FROM selected_items`); 
  code>  pre> 
  
 

当我在php myadmin中尝试此操作时,我收到以下警告而没有输出。 p>

您的SQL语法中有错误; 检查与MySQL服务器版本对应的手册,以便在第2行的''附近使用正确的语法。 p> blockquote>

当我从我的android运行我的php脚本时 应用程序它什么都不返回。有一个地方我可以检查我的查询,如PHP代码检查器。 p>

我是编程的新手,所以欢迎任何帮助和建议。谢谢你:) p> div>

As Saty already mentioned in a comment: you probably mean:

SELECT * FROM selected_items GROUP BY invoiceNo;

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.

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);