I am writing a Java program from a DB to PostgreSQL, I need to get a table and replace it with data from another table.
Picture for example:
Need the SQL query itself
Now the request is:
SELECT * FROM public.\"Court\"; I am writing a Java program from a DB to PostgreSQL, I need to get a table and replace it with data from another table.
Picture for example:
Need the SQL query itself
Now the request is:
SELECT * FROM public.\"Court\"; You need to use JOIN
SELECT T1.key, T2.name FROM table1 T1 JOIN table2 T2 ON T1.name = T2.key; You can read more in the documentation.
Like that. By the way, the previous answer was also correct, but personally I prefer to do so as below, because using the upper method, to change each parameter, a subquery will be executed, and here you can immediately get the necessary data set in one subquery. For this example (only 1 field changes) is not significantly different.
CREATE TABLE #data1 (id int, name varchar(max)) CREATE TABLE #data2 (id int, name varchar(max)) INSERT INTO #data1 SELECT 1,'11' union all SELECT 2,'22' union all SELECT 3,'33' INSERT INTO #data2 SELECT 1,'aa' union all SELECT 2,'bb' union all SELECT 3,'ww' select 'Первичные данные', * from #data1 select 'Первичные данные', * from #data2 UPDATE #data1 SET #data1.name = z.name FROM (SELECT z.id, z.name FROM #data2 z) z WHERE z.id = #data1.id select 'Данные после преобразования', * from #data1 select 'Данные после преобразования', * from #data2 Use join tables.
If you have key columns from the first table and key from the second table have the same set of values, then use the inner join (or just join ):
select t1.key, t2.name from table1 t1 join table2 t2 on t1.key = t2.key If you have a key column from the first table, for example, has some values that are not in the key column from the second table, then use the left join outer left join :
select t1.key, t2.name from table1 t1 left join table2 t2 on t1.key = t2.key An example on sqlfiddle .
Source: https://ru.stackoverflow.com/questions/573419/
All Articles