简体   繁体   中英

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.

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.

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").

The end result we are attempting to create a json file similar to the one below.

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

SQL Fiddle

MySQL 5.6 Schema Setup :

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 :

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 |

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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