聯表查詢 列名無效
用到聯表查詢SQL語句時 最后運行輸出總是報錯 說s.Jprice列名無效?? 但這條語句在sqlserver中是可以執行的
到底是怎么回事呀? 求解? 謝謝各位大神
Jprice,Mprice是SPinfo表中的進價和賣價? xscout是XSjilu表中的銷售數量? iid是兩個表中的主外鍵關聯
public void SelectSPpriceAll() throws SQLException {
?? ??? ?float Jprice;
?? ??? ?float Mprice;
?? ??? ?int xscout;
?? ???? float sum = 0;
?? ??? ?Connection con = Database.getCon();
?? ??? ?Statement sta = con.createStatement();
?? ??? ?String sql = "select SUM((s.Mprice-s.Jprice)* x.xscout) from SPinfo s,XSjilu x where s.iid=x.iid";
?? ??? ?ResultSet res = sta.executeQuery(sql);
?? ??? ?while (res.next()) {
?? ??? ??? ?Jprice = res.getFloat("Jprice");
?? ??? ??? ?Mprice = res.getFloat("Mprice");
?? ??? ??? ?xscout = res.getInt("xscout");
?? ??? ??? ?sum += (Mprice - Jprice) * xscout;
?? ??? ??? ?System.out.println("所有商品的盈利總額為:" + sum + "元");
?? ??? ?}
?? ?}
2017-03-14
查詢所得的結果集即是價錢總和了,直接獲取就行
如果要按照你寫的,那么就需要把查詢語句改為
select s.Mprice,s.Jprice,x.xscout
from SPinfo as s,XSjilu as x
where s.iid = x.iid;