文章目录
- 查找:生产ID为DTNTR物品的供应商,生产的其他物品。
- 子查询
- 自联结
- 比较
查找:生产ID为DTNTR物品的供应商,生产的其他物品。
子查询
SELECT
prod_id,
prod_name
FROM
products
WHERE
vend_id = ( SELECT vend_id FROM products WHERE prod_id = 'DTNTR' );
自联结
SELECT p1.prod_id,p1.prod_name # 必须指定为p1.prod_id,p1.prod_name
FROM
products AS p1,
products AS p2
WHERE
p1.vend_id = p2.vend_id
AND p2.prod_id = 'DTNTR'
p2已经被限制为了具体的一行
SELECT p2.prod_id,p2.prod_name FROM products AS p1, products AS p2 WHERE p1.vend_id = p2.vend_id AND p2.prod_id = 'DTNTR'
SELECT * FROM products AS p1, products AS p2 WHERE p1.vend_id = p2.vend_id AND p2.prod_id = 'DTNTR'
比较
从运行时间看,自联结比子查询快!