Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

337
Views
Intercambio de valores de columna en PostgreSQL

En mi base de datos PostgreSQL , tengo una tabla con dos valores de texto, t1 y t2 :

 | id | t1 | t2 | | 1 | abcd | xyz | | 2 | aazz | rst | | 3 | fgh | qwerty |

Me gustaría intercambiar los valores de las columnas t1 y t2 para cada fila de la tabla de manera que, usando el ejemplo anterior, este sería el resultado:

 | id | t1 | t2 | | 1 | xyz | abcd | | 2 | rst | aazz | | 3 | qwerty | fgh |

Además, supongamos que los valores de todas las filas con id=4 en adelante (4, 5, 6...) ya son correctos, ¿es posible filtrar qué filas quiero intercambiar?
Intenté esto (para bases de datos MySQL) pero ninguna de las soluciones funcionó.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Eso es una ACTUALIZACIÓN simple:

 update the_table set t1 = t2, t2 = t1 where id < 4;

A diferencia de MySQL, Postgres hace esto correctamente.

over 4 years ago · Santiago Trujillo Report

0

select * from swapit; id | t1 | t2 ----+-------+-------- 1 | abcd | xyz 2 | aazz | rst 3 | fgh | qwerty 4 | first | second 5 | first | second (5 rows) update swapit set t1 = t2, t2 = t1 where id <= 3; UPDATE 3 select * from swapit order by id; id | t1 | t2 ----+--------+-------- 1 | xyz | abcd 2 | rst | aazz 3 | qwerty | fgh 4 | first | second 5 | first | second (5 rows)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!