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

164
Views
Cómo obtener y fusionar datos de acuerdo con groupby Mysql Php

Estoy trabajando con mysql y php y quiero obtener datos de acuerdo con la "ciudad" en la matriz Aquí está mi tabla "comerciantes"

 merchantid localityId 1 30 2 30 3 31 4 30 5 32

Aquí está la tabla "localidad"

 localityId cityId name 30 1 abc 31 1 xyz 32 2 xya

Aquí está la tabla "ciudad"

 cityId name 1 Chandigarh 2 Panchkula 3 Delhi 4 Mumbai

Y quiero obtener datos de acuerdo con "ciudad/localidad", por ejemplo, quiero obtener todos los registros de la ciudad (grupo por ciudad), en otras palabras, quiero seguir el resultado, ¿cómo puedo hacer esto?

 { "Status": "1", "data": [ { "localityName": "Chandigarh", "FilterType": "filter", "result": [ { "id": "1", "name": "", //other info }, { "id": "2", "name": "", //other info }, "localityName": "Panchkula", "FilterType": "filter", "result": [ { "id": "3", "name": "", //other info }, { "id": "4", "name": "", //other info }, ...And so on }

Intenté con el siguiente código pero me dio una lista de localidad/ciudad, no me dio el resultado que quiero, ¿dónde me equivoco?

 $this->db->select('l.localityId,c.name,c.cityId'); $this->db->from('merchants m'); $this->db->join('locality l', 'l.localityId=m.localityId'); $this->db->join('city c', 'c.cityId=l.cityId'); $this->db->group_by('l.cityId'); $query = $this->db->get(); if($query->num_rows() != 0) { return $query->result_array(); } else { return false; }
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Puedes probar con el siguiente código

 $this->db->select('l.localityId,l.name') ->from('merchants m') ->join('locality l', 'l.localityId=m.localityId') ->group_by('m.localityId') ->limit($limit); $query = $this->db->get(); if($query->num_rows() != 0) { return $res=$query->result_array(); } else { return false; }
over 4 years ago · Santiago Trujillo Report

0

Puedes probar esto:

 SELECT c.cityId,c.name,GROUP_CONCAT(DISTINCT(CONCAT(l.localityId))) from locality l JOIN merchants m on l.localityId=m.localityId JOIN city c on c.cityId=l.cityId GROUP BY l.cityId;

Demostración en vivo aquí: SQL FIDDLE

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!