yyf634540328 2014-03-24 06:26 采纳率: 0%
浏览 730

​如何实现这个表四,total要求不大于80出现在表四,有not的出现在其他2个属性中.

id主键 emid hair face

1 001 good good

2 005 not good

3 018 good good

4 019 good good

5 020 not not

表一table1

id主键 emid within tone

1 001 good good

2 005 not good

3 016 good good

4 018 good good

5 020 not not

表二table2

id emid total

1 001 75

2 005 80

3 016 90

4 018 68

5 020 72

表三table3

​要求用sql语句进行插入,成为表4

id emid table1 total table2

1 001 75

2 005 hair not within not

3 018 68

4 020 hair not face not 72 within not tone not

5

​如何实现这个表四,total要求不大于80出现在表四,有not的出现在其他2个属性中.

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-01 21:48
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    要实现这个需求,您可以使用SQL查询来连接三个表格并计算每个条目的total。然后,将结果存储在新的表格中,并确保total小于等于80且不包含特定条件。

    以下是可能的SQL查询:

    INSERT INTO Table4 (ID, EMI, Total)
    SELECT 
        t1.ID,
        t2.EMI,
        SUM(t1.Total + t2.Total) AS Total
    FROM 
        Table1 t1
    JOIN 
        Table2 t2 ON t1.EMI = t2.EMI
    WHERE 
        t1.Tone = 'good' AND t2.Tone = 'good'
    GROUP BY 
        t1.ID, t2.EMI
    HAVING 
        Total <= 80 AND NOT EXISTS (
            SELECT 
                *
            FROM 
                Table3
            WHERE 
                ID IN (t1.ID, t2.ID) AND
                Total > 80 AND 
                Tone = 'good'
        )
    

    在这个查询中:

    • Table1Table2分别包含了emidtone
    • Table3包含了face
    • 使用INNER JOINTable1Table2连接在一起,基于它们共享的EMI字段。
    • 使用GROUP BYIDEMI对数据分组。
    • 使用HAVING筛选出满足总和小于等于80并且不包括任何与tone为'good'的所有条目(通过子查询检查)的数据行。
    • 最后,根据Total列添加新行到Table4

    请注意,这只是一个基本示例。您可能需要根据您的具体需求调整此查询。例如,如果您想要处理更复杂的逻辑或不同的条件,请考虑修改子查询中的条件或优化查询结构。

    评论

报告相同问题?