php - Mysql从两个表中选择组相似的行

标签 php mysql sql

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

我有两个 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 数据库中,因此该操作必须只执行一次。

谁能给我一个解决方案?

谢谢!

最佳答案

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

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

这是一个镜头:)

$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);

关于php - Mysql从两个表中选择组相似的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23637790/

相关文章:

javascript - 在 EXT js 中对渲染/转换值进行排序

php - 在 php 中使用循环创建 css/html 元素的想法

php - 如何使用 MySQL 表中的数据制作富文本格式或 PDF 文件?

php - 将整个 phpmyadmin 数据库导入新系统

MySQL 8.0 : How to Alter Procedure Definer

java - Mysql 更新似乎有效,但抛出语句关闭错误

php - 如何在 php native 中添加撇号

PHP收件箱系统

c# - 如何仅在 SQLite 中更新 1 行

mysql - 如何编写一个 SQL INSERT 查询来避免重复某一行范围内的数据