SQL:子查询返回多行

时间:2016-03-17 10:15:52

标签: mysql sql

你好我现在创建了应用程序,我的任务是每天关闭的收银台装载

但是当我运行查询时,我在Mysql中的Query中遇到了问题 pnpmyadmin给我这个消息 子查询返回超过1行

这是我的SQL查询:

select tblcashieropeningbalance.Mount as COB,

   (Select tblcashiers.CashierName  from 
  tblcashiers WHERE tblcashiers.CashierID = tblcashieropeningbalance.CashierID ) as  CashierName ,

 (Select tblcashdeposit.Mount  from 
  tblcashdeposit WHERE tblcashdeposit.CashierID = tblcashieropeningbalance.CashierID ) as  CD ,

  (Select  tblbankcashiertransfer.Mount  from 
  tblbankcashiertransfer WHERE tblbankcashiertransfer.CashierID = tblcashieropeningbalance.CashierID ) as  BCT,
(Select tblsupplierrefund.Refund  from 

  tblsupplierrefund WHERE tblsupplierrefund.CashierID = tblcashieropeningbalance.CashierID ) as  SPR,

  (Select tblcustomrefund.Refund from 
  tblcustomrefund WHERE tblcustomrefund.CashierID = tblcashieropeningbalance.CashierID ) as  CUR,

 (Select  tblcustomrefund.Refund  from 
  tblcustomrefund WHERE tblcustomrefund.CashierID = tblcashieropeningbalance.CashierID ) as  CUR,

 (Select  tblcashpayments.Mount  from 
  tblcashpayments WHERE tblcashpayments.CashierID = tblcashieropeningbalance.CashierID) as  CP,


 (Select  tblcashiertransfer.Mount   from 
  tblcashiertransfer WHERE tblcashiertransfer.FromCashierID = tblcashieropeningbalance.CashierID) as  CT,

 (Select tblcashiertransfer.Mount  from 
  tblcashiertransfer WHERE tblcashiertransfer.ToCashierID = tblcashieropeningbalance.CashierID) as  CT2,

 (Select tblcustomerrefund.Refund  from 
  tblcustomerrefund WHERE tblcustomerrefund.CashierID = tblcashieropeningbalance.CashierID) as  CR,


 (Select tblcashierbanktransfer.Mount  from 
  tblcashierbanktransfer WHERE tblcashierbanktransfer.CashierID = tblcashieropeningbalance.CashierID) as  CBT,

 (Select tblexpenses.Mount  from 
  tblexpenses WHERE tblexpenses.CashierID = tblcashieropeningbalance.CashierID) as  EX,



 (Select  tblcustomespayment.Mount from 
  tblcustomespayment WHERE tblcustomespayment.CashierID = tblcashieropeningbalance.CashierID) as  CUSP

from tblcashieropeningbalance
where tblcashieropeningbalance.CashierID = '2'
and TransDate >= '2016-01-01'
and TransDate <= '2016-02-31'

我该如何解决?

1 个答案:

答案 0 :(得分:-2)

在sql查询中使用子查询时,必须确认子查询仅返回1条记录,例如,如果使用此查询:

select *, (select id from internal_table where internal_table.id =
outer_table.internal_table_id) as internal_table_id from outer_table;

在这种情况下,您必须确保查询(select id from internal_table where internal_table.id = outer_table.internal_table_id)将返回1条记录,如果您的子查询可能返回超过1条记录并且它不是您的问题,则可以使用{{1}在你的子查询中,所以对于我们的例子:

limit 1