您好,我有一个像这样的表结构
products_id | model_num | master_model_num
1 | cth001 | 0
2 | cth002 | 0
3 | cth003 | cth001
4 | cth004 | cth001
5 | cth005 | 0
6 | cth006 | cth002
我的问题
我会在表格中提供products_id
,它会让所有产品ID master_model_num
等于给定model_num
的{{1}}
我尝试了以下查询,但它没有生成我想要的结果
products_id
答案 0 :(得分:6)
SELECT pp.products_id
FROM products p
INNER JOIN products pp
ON p.model_num = pp.master_model_num
WHERE p.products_id = '1'
答案 1 :(得分:1)
岂不
SELECT products_id
FROM products
WHERE master_model_num = (SELECT model_num
FROM products
WHERE products_id = 1)
在这种情况下更有意义吗?通过在查询结尾处AND p.products_id='1'
,您可以保证只会获得一条记录。
答案 2 :(得分:1)
试试这个
SELECT
p.products_id
FROM
products p
INNER JOIN
products pp
ON
pp.products_master_model = p.products_model
答案 3 :(得分:-1)
SELECT p.products_id FROM products p,products pp where p.model_num=pp.master_model_num and p.products_id='1'