Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

167
Visualizações
How to fetch and merge data according to groupby Mysql Php

I am working with mysql and php and i want to get data according to "city" in array Here is my table "merchants"

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

Here is table "locality"

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

Here is table "city"

cityId                  name
1                       Chandigarh
2                       Panchkula
3                       Delhi
4                       Mumbai

And i want to fetch data according to "city/locality",For example i want to fetch all record of city(group by city),in other words i want like following output,how can i do this ?

{
    "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   
         }       

I tried with following code but giving me list of locality/city , not giving me result as i want,Where i am wrong ?

$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 Respostas
Responde à pergunta

0

You can try with following code

$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 Relatório

0

You can try this:

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;

Live demo here: SQL FIDDLE

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda