我可以在SQL中使用JOIN和DELETE子句吗?

时间:2016-09-29 13:19:11

标签: sql

我想知道以下查询在sql中是否有效:

DELETE FROM Reporters 
JOIN Cases ON Reporters.CaseId = Cases.ID 
WHERE Cases.Court = @Court 

我收到错误:

  

连接附近的语法不正确

4 个答案:

答案 0 :(得分:1)

DELETE r
FROM reporters as r
INNER JOIN cases as c
ON r.CaseId = c.ID 
    WHERE c.Court = @Court 

答案 1 :(得分:1)

是的,你可以,你只需要为你的表指定别名并使用它删除:

DELETE R
-- SELECT *
FROM Reporters AS R
INNER JOIN Cases AS C
    ON R.CaseId = C.ID
WHERE C.Court = @Court ;

答案 2 :(得分:1)

如果您对表格进行别名,它将起作用;

#include <stdio.h>
#include <stdlib.h>

int main (void)
{
    int row, col, numstars;
    int half, rate=1;

    printf("Please enter a positive odd integer.\n");
    scanf("%d", &numstars);

    half=(numstars-1)/2;
    for (row=0; row<numstars;row++)
    {
      if(row <= half) {
        for (col=0; col<  half-row;          ++col) printf(" ");
        for (     ; col<= half+row;          ++col) printf("*");
      } else {
        for (col=0; col<  row-half;          ++col) printf(" ");
        for (     ; col<  numstars+half-row; ++col) printf("*");
      }
      printf("\n");
    }
    return 0;
}

或者你可以使用

DELETE r
FROM Reporters AS r
INNER JOIN Cases AS c
    ON r.CaseId = c.ID
WHERE c.Court = @Court 

答案 3 :(得分:1)

提及您要从哪个表中删除。 (该解决方案特定于SQL Server)

DELETE re --if you wanted to delete the cases replace `re` with `ca`
FROM Reporters re 
JOIN Cases ca ON re.CaseId = ca.ID 
    WHERE ca.Court = @Court