Merge branch 'master' of feistymeow.org:feisty_meow
[feisty_meow.git] / production / example_apps / zippy_maps / docs / producing_location_tables_from_zip_db_v001.sql
1 # note: this code was appropriate before migration AddLatLongColumnsToLocationsTable happened in july 2017.
2 # if one wanted to take a kake zip style db to the map demo db, there would be a few changes (such as getting rid of the
3 # combining of lat and long into a single field).
4
5 # a bunch of helper bits.  real important code is at the end.
6 use zipcodes;
7 select database();
8 show tables;
9 select * from kake_zip_code;
10 select lat, lon, locationtext, country from kake_zip_code ;
11 select location, concat(lat, ',', lon), concat(locationtext, ' ', zip_code, ', ', country) from kake_zip_code where lat != 0 and lon != 0 limit 500;
12 describe locations;
13 describe categories;
14 describe categories_locations;
15
16 # testing select with concats.
17 select location, concat(locationtext, ' ', zip_code), concat(lat, ',', lon) from kake_zip_code where lat != 0 and lon != 0 limit 500;
18
19 select * from kake_zip_code limit 500;
20 select * from locations;
21 select * from categories;
22 select * from categories_locations;
23 delete from locations where id != 0;
24 ALTER TABLE locations AUTO_INCREMENT = 1;
25 delete from categories where id != 0;
26 ALTER TABLE categories AUTO_INCREMENT = 1;
27 delete from categories_locations where id != 0;
28 ALTER TABLE categories_locations AUTO_INCREMENT = 1;
29
30 select * from locations where location like '%, AK %';
31
32 # these are the important actions...
33
34 # the real business gets done by these inserts.
35 insert into locations (id, name, location, latlong, created, modified) select null, concat(location, '-', zip_code), concat(locationtext, ' ', zip_code), concat(lat, ',', lon), now(), now() from kake_zip_code where lat != 0 and lon != 0 limit 500000;
36 insert ignore into categories (id, name, created, modified, image) select null, concat(country, '-', state_prefix), now(), now(), null from kake_zip_code where lat != 0 and lon != 0 limit 500000;
37 # big one, getting the categories done automatically...
38 insert ignore into categories_locations (id, location_id, category_id, created, modified) 
39  select null, locations.id as location_id, categories.id as category_id, now(), now() from locations inner join categories where locations.name like concat('%', categories.name, '%') limit 500000;
40