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:

enter image description here

Need the SQL query itself

Now the request is:

 SELECT * FROM public.\"Court\"; 
  • and your thoughts in the form of a request or otherwise can be seen? - Alex.B
  • 3
    I agree to the author of the previous comment, your tasks from scratch do not solve for you here without trying to solve them. Read about the operator JOIN. - Aleksei Chibisov
  • What do you mean by "replace data"? They should be new in the table on the disk or you want to display this data without changing it in the database itself. And it is impossible to call the table - it’s a table, not a function ... - Mike
  • output this data without changing the database itself - Buharin

3 answers 3

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 .