Mysql:无法从查询中的选择查询中获取值

时间:2012-06-20 23:53:48

标签: mysql

我有以下查询。如果我运行它,我会收到此错误message

查询 -

SELECT account_name,ABC,date FROM entries
    LEFT JOIN accounts ON accounts.id = entries.accounts_id
    LEFT JOIN voucher ON voucher.id = entries.trans_id 
    WHERE trans_id IN ( SELECT trans_id, amount AS ABC FROM entries 
                         WHERE accounts_id='$accounts_id' AND side='C') 
    AND accounts_id!='$accounts_id' AND side='D'
    AND voucher.date between '$dateragne1' AND '$dateragne2'

我认为问题在于值ABC。它无法从第二个查询中获取值。

您能否告诉我如何解决此问题?

提前致谢:)

1 个答案:

答案 0 :(得分:1)

试试这个:

SELECT account_name, _inner.ABC, date 
  FROM 
  (
     SELECT amount AS ABC FROM entries 
       WHERE accounts_id='$accounts_id' AND side='C'
  ) AS _inner, entries
  LEFT JOIN accounts ON accounts.id = entries.accounts_id
  LEFT JOIN voucher ON voucher.id = entries.trans_id
    WHERE trans_id IN 
    ( 
      SELECT trans_id FROM entries WHERE accounts_id='$accounts_id' AND side='C'
    ) 
AND accounts_id!='$accounts_id' AND side='D'
AND voucher.date between '$dateragne1' AND '$dateragne2'`

注意:

  • 使用这样的子查询不允许您从中请求字段。
  • 此外,IN语句仅使用来自列的数据,而不是两列。