我需要从表中删除一堆记录。我找到了一个可以完成这项工作的查询,但我被告知不支持子查询,但是连接是..是否可以将以下查询转换为连接。如果是这样的话?
SELECT *
FROM PRODUCT
WHERE PROD_NAME IN
(SELECT PROD_NAME
FROM PRODUCT
WHERE BRAND = 'Apt88'
AND NAME = 'Version'
AND VALUE IN ('3.7', '3.8'))
任何帮助表示赞赏,
泰德
答案 0 :(得分:2)
你是对的 - 在MySQL中不支持从你选择的同一个表中删除。但你可以用另一个子查询来欺骗它
DELETE FROM PRODUCT
WHERE PROD_NAME IN
(
select * from
(
SELECT PROD_NAME
FROM PRODUCT
WHERE BRAND = 'Apt88' AND NAME = 'Version' AND VALUE IN ('3.7', '3.8')
) x
)
答案 1 :(得分:0)
是的,您可以将产品表加入到自身中。您只需使用两个单独的别名来区分查询中逻辑上独立的表:
SELECT p1.*
FROM PRODUCT p1
INNER JOIN PRODUCT p2 on p1.PROD_NAME = p2.PROD_NAME
WHERE p2.BRAND = 'Apt88'
AND p2.NAME = 'Version'
AND p2.VALUE IN ('3.7', '3.8')
答案 2 :(得分:0)
这是正确的等价物:
SELECT p.*
FROM PRODUCT p join
(SELECT distinct PROD_NAME
FROM PRODUCT
WHERE BRAND = 'Apt88' AND NAME = 'Version' AND VALUE IN ('3.7', '3.8')
) pn
on p.prod_name = pn.prod_name;
请注意distinct
的使用。如果您的行不同(但不一定是prod_name
),那么您可以执行以下操作:
SELECT distinct p.*
FROM PRODUCT p join
PRODUCT pn
on p.prod_name = pn.prod_name;
WHERE pn.BRAND = 'Apt88' AND pn.NAME = 'Version' AND pn.VALUE IN ('3.7', '3.8');