3

这是两个表 Fruit,两个表Fruit_types之间存在 am:n 关系,所以我们有第三个表 fruit_type_fruit,它具有上面的主键two tables。桌子看起来像这样

Fruit
ID    NAME    
1    A
2    B
3    C

Fruit_type
ID    LABEL
1    CITRIC
2    DRUPES
3    UNCATALOGUED

Fruit_type_Fruit
Fruit_id    Fruit_type
1        1
1        2
1        3
2        1
3        3

问题陈述:一些水果即使它们有一个类别(即标签),也会得到标签 Uncatalogued

例如:-
A 获得以下标签:柠檬酸、核果和未编目。

    B has    citric ,

    C has    Uncatalogued. 

现在我需要一个查询来删除所有具有合适标签但仍具有未编目标签的记录。在上面的例子中

A record which is uncatalogued should be deleted and not

A    Citric and    Drupes neither

C    Uncatalogued. 
4

1 回答 1

2

这样的事情怎么样

SQL 小提琴演示

DELETE ftf
FROM fruit_type_fruit ftf
WHERE Fruit_type_ID = 3
AND Fruit_ID IN
    (
            SELECT *
            FROM (
                    SELECT DISTINCT Fruit_ID
                    FROM fruit_type_fruit f 
                    WHERE f.Fruit_type_ID = 3
             ) ss
            WHERE Fruit_ID IN (
                    SELECT *
                    FROM (
                    SELECT DISTINCT Fruit_ID
                    FROM fruit_type_fruit f 
                    WHERE f.Fruit_type_ID <> 3
             ) s)
  )
于 2012-10-08T04:39:58.060 回答