简体   繁体   English

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

[英]Mysql: Unable to fetch value from a select query inside a query

I have the following query. 我有以下查询。 If I run it I get this error message . 如果我运行它,则会收到此错误消息

Query- 查询-

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'

I think the problem is with the value ABC . 我认为问题在于价值ABC It is unable to fetch the value from the second query. 它无法从第二个查询中获取值。

Could you please tell me how to fix this query? 您能告诉我如何解决此查询吗?

Thanks in Advance :) 提前致谢 :)

Try this: 尝试这个:

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'`

Notes: 笔记:

  • Using subquery like this doesn't allow you to request a fields from it. 使用这样的子查询不允许您从中请求字段。
  • Also, IN statement use data from only only column, not two. 另外, IN语句仅使用来自列的数据,而不使用两个数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM