當前位置:首頁 » 編程語言 » sql自聯

sql自聯

發布時間: 2025-08-25 17:30:00

sql中 in的用法

你現在的這個要求一般不用in.

假如你說的大類C是指大類的名稱是"C", 則用下面的語句:

select c.*
from category a,category b, proct c
where a.name='C' and a.pid=0
and b.pid=a.cid
and c.cid=b.cid

首先從category中取出name為'C'且pid為0的數據(得到cid), 表映射到a;
然後以自聯,再從category中取得pid為a的cid的數據(也就是大類C中的所有小類,表映射到b;
最後再關聯產品表proct, 取得cid為b的cid的所有產品,表映射到c.

當然, 如果你所說的C大類, 直接就知道其id號, 就像你給的例子中直接知道了10, 那麼"自聯"這步就不需要了.

-------------------------------

如果非要用in, 則按下面的方式:

select * from proct where cid in (
select b.cid
from category a,category b
where a.name='C' and a.pid=0
and b.pid=a.cid
)

-------------------------------

另外, in 還用在下面這種情況, 比如:

你想取得1,3,8,9這四個小類的所有產品, 則可以用:

select * from proct where cid in (1,3,8,9)

熱點內容
java返回this 發布:2025-10-20 08:28:16 瀏覽:585
製作腳本網站 發布:2025-10-20 08:17:34 瀏覽:881
python中的init方法 發布:2025-10-20 08:17:33 瀏覽:574
圖案密碼什麼意思 發布:2025-10-20 08:16:56 瀏覽:761
怎麼清理微信視頻緩存 發布:2025-10-20 08:12:37 瀏覽:677
c語言編譯器怎麼看執行過程 發布:2025-10-20 08:00:32 瀏覽:1005
郵箱如何填寫發信伺服器 發布:2025-10-20 07:45:27 瀏覽:249
shell腳本入門案例 發布:2025-10-20 07:44:45 瀏覽:108
怎麼上傳照片瀏覽上傳 發布:2025-10-20 07:44:03 瀏覽:799
python股票數據獲取 發布:2025-10-20 07:39:44 瀏覽:705