join on two different table's column sqlite3

本小妞迷上赌 提交于 2019-12-13 05:10:04

问题


hi have a two table one table contain column named

Table A _id, Eng, Hindi

other table contain

Table B _id, Eng, Hindi

i want to join two table by matching word with Eng if Eng word is not match it will be added into Table A which is from Table B otherwise Table A's value will be remain

for example

Now Table B

Table A

The Result would be on TABLE A is


回答1:


FULL JOIN is a term to combine rows from multiple table. Does not apply to your needs.

You only need to insert into table a all entries in table b which are not in table a.

INSERT INTO TABLE a(Eng, Hindi) SELECT Eng, Hindi FROM b WHERE eng NOT IN (SELECT eng FROM a);



回答2:


If you just want to get (SELECT) the desired output and TableA can contain records that are not present in TableB then you can emulate FULL JOIN to achieve your goal

SELECT e.eng, COALESCE(a.hindi, b.hindi) hindi
  FROM
(
  SELECT eng FROM TableB 
   UNION 
  SELECT eng FROM TableA
) e LEFT JOIN TableB b 
    ON e.eng = b.eng LEFT JOIN TableA a
    ON e.eng = a.eng

If on the other hand TableA always contains only a subset of eng values of TableB then you can just use LEFT JOIN

SELECT b.eng, COALESCE(a.hindi, b.hindi) hindi
  FROM TableB b LEFT JOIN TableA a
    ON b.eng = a.eng

Here is SQLFiddle demo


Now if you want to update the content of TableA and assuming that A_id is AUTOINCREMENTyou can do

INSERT INTO TableA (eng, hindi)
SELECT b.eng, b.hindi
  FROM TableB b LEFT JOIN TableA a
    ON b.eng = a.eng
 WHERE a.eng IS NULL

Here is SQLFiddle demo

or

INSERT INTO TableA (eng, hindi)
SELECT b.eng, b.hindi
  FROM TableB b
 WHERE NOT EXISTS
(
  SELECT * 
    FROM TableA
   WHERE eng = b.eng
);

Here is SQLFiddle demo



来源:https://stackoverflow.com/questions/19356996/join-on-two-different-tables-column-sqlite3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!