子查询无法正常工作

时间:2017-01-05 16:22:48

标签: mysql sql subquery

我目前正在创建一个数据库并尝试使用各种查询。但我似乎无法让我的子查询起作用。

下面的代码显示了我当前的代码当客户ID = 21345432时,我正在尝试获取客户的姓名,地址和电话,而不是显示屏向我显示所有客户,而不仅仅是特定的客户。

代码:

select Customer_Name, Customer_Address, Customer_Telephone
from CustomerInfo
where (Select CustomerID from CustomerInfo where CustomerID = "21345432");

2 个答案:

答案 0 :(得分:2)

如果您确实需要子查询,它将如下所示:

select Customer_Name, Customer_Address, Customer_Telephone
from CustomerInfo
where specialID = (Select specialID from sometable);
  

请记住,如果子查询提供了多个结果,则会产生错误。如果你想要多个结果,那么你真的想要一个连接。 (或使用in像Prdp的答案)

答案 1 :(得分:1)

您不需要sub-query

SELECT Customer_Name,
       Customer_Address,
       Customer_Telephone
FROM   CustomerInfo
WHERE  CustomerID = 21345432 

双引号用于标识符。对于字符串文字,使用单引号,可以直接添加整数值

更新:(只是使用子查询的示例)

SELECT Customer_Name,
       Customer_Address,
       Customer_Telephone
FROM   CustomerInfo
WHERE  CustomerID in (SELECT CustomerID
                     FROM   CustomerInfo
                     WHERE  CustomerID = 21345432);