简体   繁体   English

mySql - 从表中获取相关值

[英]mySql - get related values from table

OK I'm gonna try to be as clear as I can, but bear with me - its been a long week :) 好吧,我会尽可能地保持清醒,但请忍受我 - 这是一个漫长的一周:)

We have a table that is structured like below: 我们有一个表格,结构如下:

+----+----------+---------+------------------+
| id | field_id | user_id |      value       |
+----+----------+---------+------------------+
|  1 | Country  |       2 | USA              |
|  2 | Country  |       3 | USA              |
|  3 | Country  |       4 | CA               |
|  4 | Country  |       5 | MX               |
|  5 | Province |       2 | FL               |
|  6 | Province |       3 | GA               |
|  7 | Province |       4 | British Columbia |
|  8 | Province |       5 | Sonara           |
|  9 | City     |       2 | Orlando          |
| 10 | City     |       3 | Brunswick        |
| 11 | City     |       4 | Vancouver        |
| 12 | City     |       5 | Nogalas          |
+----+----------+---------+------------------+`

and we are needing (hoping for) a query to return all Country - State/Province - City combinations for the purpose of dynamically generating a JSON file. 我们需要(希望)一个查询返回所有Country - State / Province - City组合,以便动态生成JSON文件。

Something to the effect of 有效的东西

"SELECT all Provinces and Cities WHERE Country = 'USA'"

(but of course, with the structure of our db table - which unfortunately can't be changed - its quite a bit more complicated. (但当然,我们的db表的结构 - 遗憾的是无法更改 - 它的复杂程度要高得多。

Any of the values that share a common "user_id" can safely be assumed to "go together" (ie. user_id 2 has "USA" for "Country", "FL" for "Province", and "Orlando" for "City"). 共享共同“user_id”的任何值可以安全地假设为“一起”(即,user_id 2具有“USA”表示“Country”,“FL”表示“省”,“Orlando”表示“City” )。

The end result we are attempting to create a json file similar to the one below. 最终结果我们试图创建一个类似于下面的json文件。

{
"USA": {
    "Florida": [
        {"City": "Orlando"},
        {"City": "Palm Beach"}
    ],
    "Georgia": [
        {"City": "Atlanta"},
        {"City": "Brunswick"}
    ]
}, 
"Canada": {
    "Alberta": [
        {"City": "Calgary"}
    ],
    "Ontario": [
        {"City": "Atlanta"},
        {"City": "Brunswick"}
    ]
}
}

With the result of this query and some loop you should be able to generate your Json as you want 有了这个查询和一些循环的结果,你应该能够根据需要生成你的Json

SQL Fiddle SQL小提琴

MySQL 5.6 Schema Setup : MySQL 5.6架构设置

CREATE TABLE t
    (`id` int, `field_id` varchar(8), `user_id` int, `value` varchar(16))
;

INSERT INTO t
    (`id`, `field_id`, `user_id`, `value`)
VALUES
    (1, 'Country', 2, 'USA'),
    (2, 'Country', 3, 'USA'),
    (3, 'Country', 4, 'CA'),
    (4, 'Country', 5, 'MX'),
    (5, 'Province', 2, 'FL'),
    (6, 'Province', 3, 'GA'),
    (7, 'Province', 4, 'British Columbia'),
    (8, 'Province', 5, 'Sonara'),
    (9, 'City', 2, 'Orlando'),
    (10, 'City', 3, 'Brunswick'),
    (11, 'City', 4, 'Vancouver'),
    (12, 'City', 5, 'Nogalas')
;

Query 1 : 查询1

SELECT 
    tc.`value` as Country,  
    tp.`value` as Province,  
    tcy.`value` as City
FROM (
        SELECT `value`, user_id FROM t WHERE field_id = "Country"
    ) as tc
    LEFT JOIN (
            SELECT `value`, user_id FROM t WHERE field_id = "Province"
        ) as tp
        ON tc.user_id = tp.user_id
    LEFT JOIN (
            SELECT `value`, user_id FROM t WHERE field_id = "City"
        ) as tcy
        ON tp.user_id = tcy.user_id

Results : 结果

|Country|         Province |      City |
|-------|------------------|-----------|
|   USA |               FL |   Orlando |
|   USA |               GA | Brunswick |
|    CA | British Columbia | Vancouver |
|    MX |           Sonara |   Nogalas |

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM