當前位置:首頁 » 編程語言 » 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)

熱點內容
python列印日誌 發布:2025-08-25 19:37:00 瀏覽:413
vr哪個配置最好 發布:2025-08-25 19:36:51 瀏覽:836
解壓擠痘視頻 發布:2025-08-25 19:34:31 瀏覽:308
老鼠腳本是什麼意思 發布:2025-08-25 19:27:59 瀏覽:476
androidlongstring 發布:2025-08-25 19:25:00 瀏覽:494
app製作androidstudio 發布:2025-08-25 19:14:12 瀏覽:241
hibernate連接資料庫 發布:2025-08-25 18:55:37 瀏覽:719
c語言hook 發布:2025-08-25 18:44:16 瀏覽:700
解壓瓶子 發布:2025-08-25 18:10:17 瀏覽:270
五年級c語言 發布:2025-08-25 18:08:42 瀏覽:542