每个派生表必须有自己的别名吗?

时间:2012-09-22 13:51:14

标签: c# mysql

  

可能重复:
  every derived table must have its own alias

我有两个相同列的MYSQL表我试图获得总和qtd,其中produto是相同但我有错误“每个派生表必须有自己的别名”plz help

MySqlCommand cmd = cnn.CreateCommand();
                    StringBuilder query = new StringBuilder();
                    query.Append("select produto,sum(qtd) from ( ");
                    query.AppendFormat(" select x.produto,x.qtd from itens_venda x where x.data='{0:MM/dd/yyyy}'", new object[] { data1 });
                    query.Append(" Union all ");
                    query.AppendFormat(" select y.produto,y.qtd from itens_venda_fiscal y where y.data='{0:MM/dd/yyyy}'", new object[] { data1 });
                    query.Append(" )  group by produto order by sum(qtd) desc  ");
                    cmd.CommandText = query.ToString();
                    MySqlDataReader reader = cmd.ExecuteReader();
                    while ( reader.Read() )
                    {
                        Codigo.Add(reader.GetString(0));
                        Quantidade.Add(reader.GetString(1));
                    }

1 个答案:

答案 0 :(得分:0)

是的,每个派生表都必须有自己的别名。添加如下所示的别名。

query.Append(" ) as t group by produto order by sum(qtd) desc  ");