更新&插入一个查询(不是upsert)

时间:2017-10-26 22:41:38

标签: database postgresql

我正在使用PostgreSQL。

我有桌子A,B,C& d。

表A有一个表B的外键(所以一对多)。

表C& D具有表A的外键(一对多)。 所以它看起来像:

B: - >答: - > C& d

现在我想添加一个中间表E来建立关系:

E: - > C& D(一对多)

B - > E(与fk一对一)

B: - > A(一对多)

我需要它来引入表F,表F也有表E的外键。

表E只有一个标识列'id'。

现在我必须编写一个迁移,并且不知道如何向E​​添加行并同时更新B

在伪SQL中,它必须如下所示:

更新B SET A_id = id INSERT INTO A(id)

2 个答案:

答案 0 :(得分:1)

您可以使用WITH语句:

WITH inserted_rows AS (
    INSERT INTO A(...) VALUES (...) RETURNING *
) UPDATE B SET A_id = a.id FROM inserted_rows a WHERE ...;

WHERE子句应过滤B的匹配行。不幸的是,你的问题很广泛,所以我不能给你一个更详细的例子。

答案 1 :(得分:-1)

添加临时列可以帮助我。

package com.aryvart.myaromasupply;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.ListView;
import com.aryvart.myaromasupply.Adapter.CartListAdapterKV;
import com.aryvart.myaromasupply.Bean.CommonBean;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

/**
 * Created by android01 on 28/8/17.
 */

public class CartPageKV extends Activity implements MyInterface {
    String json = null;
    List<CommonBean> movieList = new ArrayList<>();
    RecyclerView recyclerView;
    CartListAdapterKV mAdapter;
    Context context;
    CoordinatorLayout coordinatorLayout;
    Button btn_submit;
    HashMap<String, JSONObject> hsFilterGmap;
    String value;
    JSONArray jsonArray;
    ListView llView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cart_page_kv);
        context = this;
        recyclerView = (RecyclerView) findViewById(R.id.my_recyclerView);
        btn_submit = (Button) findViewById(R.id.btn_submit);
        coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinator_layout);
        llView = (ListView) findViewById(R.id.ll_view);
        loadJSONFromAsset();

        //Response API
        try {
            JSONObject obj = new JSONObject(json);
            Log.e("NN", "json-->" + obj.toString());

            JSONArray respArray = obj.getJSONArray("results");
            Log.e("NN", "respArray-->" + respArray.toString());
            for (int i = 0; i < respArray.length(); i++) {
                JSONObject jsonObj = respArray.getJSONObject(i);
                CommonBean drawerBean = new CommonBean();
                drawerBean.setStr_cart_id(jsonObj.getString("id"));
                drawerBean.setStr_cart_title(jsonObj.getString("name"));
                drawerBean.setStr_quan(jsonObj.getString("quantity"));
                drawerBean.setStr_tot_quant(jsonObj.getString("total_quantity"));
                movieList.add(drawerBean);
            }

            // Getting adapter by passing xml data ArrayList
            mAdapter = new CartListAdapterKV(movieList, context, (MyInterface) context);
            llView.setAdapter(mAdapter);


        } catch (JSONException e) {
            e.printStackTrace();
        }

        //Button Click Event

        btn_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e("NN:fc", String.valueOf(hsFilterGmap));
                Iterator myVeryOwnIterator = hsFilterGmap.keySet().iterator();
                JSONArray jsonArray = new JSONArray();
                while (myVeryOwnIterator.hasNext()) {
                    String key = (String) myVeryOwnIterator.next();
                    JSONObject value1 = hsFilterGmap.get(key);
                    Log.e("NN:value", value1.toString());
                    jsonArray.put(value1);

                }

                Log.e("NN:fcAr", jsonArray.toString().replaceAll("\\\\", ""));
                System.out.println("the JSON ARRAY is" + jsonArray.toString());

            }
        });
    }

    public String loadJSONFromAsset() {

        try {

            InputStream is = getAssets().open("data.json");
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            json = new String(buffer, "UTF-8");


        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
        return json;

    }


    // Interface to get Checked value from listview(Hashmap for not allowing duplicates)
    @Override
    public HashMap<String, JSONObject> getUnCheckedVal(HashMap<String, JSONObject> strVal, String str_removed_id) {

        hsFilterGmap = strVal;
        Log.e("NN:fc", String.valueOf(hsFilterGmap));
        return hsFilterGmap;
    }
}
相关问题