【问题标题】:Mysql Select group similar rows from two tableMysql从两个表中选择组相似的行
【发布时间】:2014-07-01 12:18:57
【问题描述】:

如果有人能帮助我解决我的问题,我将不胜感激。

我有那两个mysql表:

城市(约 90.000 行)

|         ID(primaryKey)         |  CITY_ID |  SUPPLIER   |
|3fe07eaec62d209c1c153bd3e5501ac5|  xxxxxx  |  supplier1  |
|2a3eaad85c0e7ebb1f7ddaf9a423d808|  yyyyyy  |  supplier2  |
|c47376be023028eb17addcd5813cf592|  zzzzzz  |  supplier3  |
|f4d2b3a0da4d6bad0c89ad2471bd1dd7|  kkkkkk  |  supplier1  |

City_Translates(大约 500.000 行)

|               ID               |  CITY    |  COUNTRY        | LANGUAGE |
|3fe07eaec62d209c1c153bd3e5501ac5|  LONDRA  |  Gran Bretagna  |    IT    |
|3fe07eaec62d209c1c153bd3e5501ac5|  LONDON  |  Great Britain  |    EN    |
|3fe07eaec62d209c1c153bd3e5501ac5|  LONDON  |  Großbritannien |    DE    |
|c47376be023028eb17addcd5813cf592|  LONDON  |  Great Britain  |    EN    |
|c47376be023028eb17addcd5813cf592|  LONDRA  |  Gran Bretagna  |    EN    |
|2a3eaad85c0e7ebb1f7ddaf9a423d808|  ROMA    |  ITALIA         |    IT    |
|2a3eaad85c0e7ebb1f7ddaf9a423d808|  ROME    |  ITALY          |    EN    |

我需要创建一个 php 脚本来对这两个表进行分组,以便具有这样的结构:

[
{"translates":
  {
    "it":{"city":"LONDRA","country":"Gran Bretagna"},
    "en":{"city":"LONDON","country":"Great Bretain"},
    "de":{"city":"LONDON","country":"Großbritannien"}
  },
 "suppliers":
  {
     "supplier1":"xxxxxx",
     "supplier2":"zzzzzz"
  }
},
{"translates":
  {
    "it":{"city":"ROMA","country":"ITALIA"},
    "en":{"city":"ROME","country":"ITALY"}
  },
 "suppliers":
  {
     "supplier1":"yyyyyy"
  }
},...]

我这样做是因为我需要将该结构导入到 noSQL 数据库中,因此必须只执行一次此操作。

谁能给我一个解决方案?

谢谢!

【问题讨论】:

  • 在您的示例中,为什么列出$array[0]->suppliers->supplier2?看起来第一条记录中的所有内容都与 ID 3fe07eaec62d209c1c153bd3e5501ac5 有关
  • 不..这只是一个例子..我需要为每种语言的城市和国家分组

标签: php mysql sql


【解决方案1】:

我对为什么 LONDON/Great Britain 引用不同的城市 ID(主键)感到困惑。无论如何,我假设您想按城市字符串和城市 id (pk) 的组合进行分组,因为您的示例每个组有多个供应商。

即我们知道LONDONLONDRA 属于同一个组,因为它们具有相同的密钥。当用不同的键再次看到它们时,我们知道它们属于同一组,因为我们之前遇到过这些城市名称。

这是一个镜头:)

$query = "SELECT * FROM Cities c 
JOIN City_Translates ct ON ct.ID = c.ID
ORDER BY ID"; //order by is necessary so all like cities end up in the same group

$rs = mysqli_query($query) or die(mysqli_error());

$results = array();
$city_id = array(); // lookup id by city name
//used to group like cities

while($r = mysqli_fetch_assoc($rs)) {

    //if we've seen this city before grab
    //the city id (pk) where we store its data
    if(isset($city_id[$r['CITY']])) {
        $id = $city_id[$r['CITY']];
    }
    else { //otherwise store the city id (pk) for this city
        $city_id[$r['CITY']] = $r['ID'];
        $id = $r['ID'];
    }

    $results[$id]['translates'][$r['LANGUAGE']] = array(
        'city' => $r['CITY'],
        'country' => $r['COUNTRY'],
    );

    $results[$id]['suppliers'][$r['SUPPLIER']] = $r['CITY_ID']; 
}

$results = array_values($results); //remove ID keys
print json_encode($results,JSON_PRETTY_PRINT);

【讨论】:

    猜你喜欢
    • 2021-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-28
    • 1970-01-01
    • 1970-01-01
    • 2015-10-30
    • 1970-01-01
    相关资源
    最近更新 更多