2013-06-28 26 views
0

我正在使用以下查询来获取客户详细信息。但它不起作用,请帮助我。我是SQL新手。在主查询中获取子查询值

select cu.fld_cust_id,ord.* from test1 where fld_order_id ord in (select * from tbl_customer cu where cu.fld_status=1); 
+0

你能明确指定你想要的吗? –

回答

1
  1. 您不能选择从WHERE子句中使用子查询列,becaused他们没有加入到这个查询。你只是使用这个子查询返回的值范围

  2. 你的子查询应该只返回一列。

  3. 你应该尝试这样的事情。

    SELECT cu.fld_cust_id,ORD * FROM test1的 JOIN tbl_customer铜ON cu.fld_status = 1 AND fld_order_id = cu.fld_cust_id

+0

fld_order_id = cu.fld_cust_id没有意义,ord也没有意义。* –

+0

你说得对,但我们实际上并不知道这些表的样子。 –

0

在查询中,你还需要知道,你可以只能看到当前范围中的字段。因此,在主查询中,您只能使用FROM TEST1,因此您只能看到该表中的字段。 ord。*和cu的使用会给出错误。如果您需要该表中的其他字段,请使用JOIN。 TEST1表应该包含一个链接到TBL_CUSTOMER的外键,如果不是,则需要使用其他表的路径或重新设计数据库。如果你有外键,这就是你身边的IN操作符用什么:

select fld_cust_id from test1 where fld_cust_id in (select id from tbl_customer cu where cu.fld_status=1); 
0

不知道什么在你的tbl_customer,但好像你是匹配fld_order_id为*。您应该匹配客户表中的order_id。

select cu.fld_cust_id,ord.* 
from test1 
where fld_order_id ord in (
    select *ORDERID* from tbl_customer cu where cu.fld_status=1 
);