SQL Join和Update查询

时间:2018-02-14 18:35:13

标签: sql sql-server database

所以我想将action列更新为Table1中的值'Insert',如果Table1和Table2中的id匹配但UID不匹配。

现在我的查询看起来像

UPDATE Table1
SET Action = 'Insert'
FROM Table1
    JOIN Table2 ON Table1.id = Table2.id
        AND Table1.UID <> Table2.UID

即使UID 不同,这是将操作设置为Insert,有人可以帮助我解释为什么这样做会这样做吗?

2 个答案:

答案 0 :(得分:2)

我的假设是你有这样的事情:

Table1
id   |  UID  |  action
1    |   1   |   bla
1    |   2   |   bleck
1    |   3   |   floop

Table2
id   |  UID  |  action
1    |   1   |   bla
1    |   2   |   bleck
1    |   4   |   floop

您希望更新Table1中的第三行,因为UID不在Table2中。

问题是Table2中的第三行符合条件Table1Table1.id = Table2.id AND Table1.UID <> Table2.UID的所有行:Table1

这意味着在这种情况下,Action = 'Insert'中的所有行都将使用NOT EXISTS()更新

我认为你想使用UPDATE T1 SET Action = 'Insert' FROM Table1 T1 WHERE NOT EXISTS (SELECT * FROM Table2 T2 WHERE T1.id = T2.id AND T1.UID = T2.UID)

Table1

编辑,更多解释加入失败的原因:

这是多对多连接,这意味着条件允许Table2中的多行匹配来自SELECT * FROM Table1 T1 JOIN Table2 T2 on T1.id = T2.id and T1.UID <> T2.UID 的多行

最简单的方法是将更新更改为选择:

id   |  UID  |  action     id   |  UID  |  action
1    |   3   |   floop     1    |   4   |   floop

您可能希望这会导致:

id   |  UID  |  action     id   |  UID  |  action
1    |   1   |   bla       1    |   4   |   floop
1    |   2   |   bleck     1    |   4   |   floop
1    |   3   |   floop     1    |   4   |   floop

但实际上它会导致:

id = 1

这意味着当您更新时,您正在Table1

中点击package sg.com.test.parseandroid; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.widget.Toast; import com.parse.FindCallback; import com.parse.ParseException; import com.parse.ParseObject; import com.parse.ParseQuery; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; private GridLayoutManager gridLayoutManager; private static CustomAdapter adapter; private List<MyData> data_list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView = (RecyclerView) findViewById(R.id.recycler_view); data_list = new ArrayList<>(); gridLayoutManager = new GridLayoutManager(this, 2); recyclerView.setLayoutManager(gridLayoutManager); ParseQuery<ParseObject> query = ParseQuery.getQuery("Post"); query.findInBackground(new FindCallback<ParseObject>() { public void done(List<ParseObject> postList, ParseException e) { if (e == null) { for (ParseObject obj : postList) { MyData data = new MyData(obj.getObjectId(), obj.getString("image_url")); data_list.add(data); Log.d("MAINACTIVITY", String.format("objectid- %s", data_list.get(postList.indexOf(obj)).getObjectId())); } } else { Toast.makeText(MainActivity.this, "Something went wrong...", Toast.LENGTH_SHORT).show(); } } }); adapter = new CustomAdapter(this, data_list); recyclerView.setAdapter(adapter); } } 的所有行

答案 1 :(得分:0)

如果您放置条件Table1.UID&lt;&gt; Table2.UID到WHERE子句中,它没有解决你的问题吗?

lev-1