更多的子查詢 我們可以使用在select查詢語句中再包括一個select子查詢語句。舉個例子吧,首先我們列除所有購買貴重物品的顧客,貴重物品的標準是比所有賣出的物品價錢的平均值多100元的物品。具體語句如下: select ownerid from antiques where price > (select avg(price) + 100 from antiques); 上面子查詢語句是計算物品的平均價格再加100元,并搜索所有在antiques表中price大于這個數值的ownerid。這里你可以使用distinct ownerid來排除復制的現象。 下面的語句列出了所有在antiqueowners表中的有買過物品的人的lastname: select ownerlastname from antiqueowners where ownerid =
from antiques); 這個子查詢返回了一系列的顧客,當且僅當物品擁有者的id出現在子查詢的列表中,古董的擁有者的lastname才會顯示出來。 為了更新這個例子,我們假設有一個買過bookcase的顧客,他的firstname在數據庫中出錯了,應該為john: update antiqueowners set ownerfirstname = 'john' where ownerid =
from antiques where item = 'bookcase'); 上面的語句中的子查詢首先搜索買過bookcase的顧客的buyerid,然后在外層的查詢中來更新他的firstname。 |
新聞熱點
疑難解答