使用连接删除mysql中的列

时间:2014-03-16 10:37:20

标签: mysql sql join sql-delete

我必须使用where通过两个表从表中仅删除列而不是整行。
这是一个例子:

delete from Rating using Movie, Rating 
where Movie.mID=Rating.mID 
  and Rating.stars<4 
  and (Movie.year<1970 or Movie.year>2000);

此查询有效,但在表'Rating'中,它会删除整行。我只想删除'stars'列中的数据。

2 个答案:

答案 0 :(得分:1)

您需要更新表格才能执行此操作。您实际上需要更新列stars可能会更改为NULL

的值
 Update stars set NULL // and go on with your query

答案 1 :(得分:0)

您需要update声明但不需要delete

更改

delete from Rating using Movie, Rating 
where Movie.mID = Rating.mID 
  and Rating.stars<4 
  and ( Movie.year < 1970 or Movie.year > 2000 );

Update Rating r, Movie m
set r.stars = 0
where m.mID = r.mID 
  and Rating.stars < 4 
  and ( m.year < 1970 or m.year > 2000 );