• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

146
Views
String_agg para múltiples columnas

tengo la siguiente selección

 $repairs = DB::select(DB::raw(' select r.id repair_id, w.name, w.surname from repairs r join repair_worker rw on r.id = rw.repair_id join workers w on w.id = rw.worker_id where r.vehicle_id = ? group by w.name, w.surname, r.id '),[$vehicle->id]);

y estoy tratando de obtener w.name y w.surname en un campo, separados por una coma, por ejemplo. Hay una función string_agg(column, ',') pero no tengo suerte para que funcione. Siempre me sale error de sintaxis.

Mi intento:

 $repairs = DB::select(DB::raw(' select r.id repair_id, string_agg(w.name, w.surname, ', ') workers from repairs r join repair_worker rw on r.id = rw.repair_id join workers w on w.id = rw.worker_id where r.vehicle_id = ? group by r.id '),[$vehicle->id]);

¿Como lo harias?

Estoy usando el Postgresql 9+ más nuevo

almost 3 years ago · Santiago Trujillo
2 answers
Answer question

0

Usa el || operador para realizar la concatenación de cadenas, luego agregue este resultado.

 $repairs = DB::select(DB::raw(" select r.id repair_id, string_agg(w.name || ' ' || w.surname, ', ') workers from repairs r join repair_worker rw on r.id = rw.repair_id join workers w on w.id = rw.worker_id where r.vehicle_id = ? group by r.id "),[$vehicle->id]);
almost 3 years ago · Santiago Trujillo Report

0

¿prueba esto?

 $repairs = DB::select(DB::raw(" select r.id repair_id, string_agg(concat(w.name, ',',w.surname), ', ') workers from repairs r join repair_worker rw on r.id = rw.repair_id join workers w on w.id = rw.worker_id where r.vehicle_id = ? group by r.id "),[$vehicle->id]);
almost 3 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 Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error