Relating rows to one another in separate table - mysql

I have a table called Neighborhoods. Some of these neighborhoods are related, and I'd like to be able to capture that relationship in another table.
For example: NYC's Union Square is a neighborhood that's near East Village. I want to perform a query for nearby neighborhoods on Union Square and get East Village. Likewise, I'd like to query nearby neighborhoods on East Village and receive Union Square.
My plan is to have a table called NearbyNeighborhoods that links neighborhood1 to neighborhood2.
Is there a smarter way to do this?

Related

Query to find lakes that are in multiple states

I have tables to hold information about Lakes, Counties, and States. I also have a table called LakeLocation that is simple a pair of foreign keys of a LakeID and CountyID, so I can know where they are. And each county has a StateName inside it.
A high-level view of it all is something like this:
Lake = {id,name,...}
County = {id, name, StateName ....}
State = {name, ...}
LakeLocation = {LakeID, CountyID}
I am trying to find out which states have lakes that are in more than one state. An example is Lake Tahoe which crosses four county lines and two state lines. I know that I will need a recursive query, but I only understand those for simple one table child/parent type relationships. Not this.
How can I design a query that does what I need? Thanks
You only need a group by to know what are the lakes
SELECT LakeID
FROM LakeLocation
GROUP BY LakeID
HAVING COUNT(CountyID) > 1
Then if you want lake names
SELECT *
FROM Lake
WHERE LakeID IN (<previous query>)

MySql total is bigger than individual elements

I have a huge MySql table that is in use in production. I have tables which are named by geographical areas with foreign keys for smaller divisions. It is structured like db_country-> tbl_city1, tbl_city2, tbl_city3 and each tbl_city has rows for households inside with other details like street, address and number of people for each household.
PROBLEM: If I pull the data for each family grouped by STREETS I get the correct number of people but when I call the number of people in the entire city, I get a slightly inflated number. I know there must be something I've messed up, what is the possible fail area in this scenario?
The first query that yields inflated output is:
SELECT sum(people) AS people FROM city WHERE division=2136 AND status=1;
and the second query that yields correct output is:
SELECT street.name, SUM(people) AS people FROM city INNER JOIN streets
ON city.street=streets.id WHERE division=2136 AND status=1
GROUP BY street.name;
The picture is the real output, above table shows combined total(inflated) while the lower one shows individual streets/villages with the correct number MYSQL OUTPUT IMAGE
There are records in city table which have no related street record matching the join city.street=streets.id.
Try this sql to find out the missed records.
Select city.* from city left join street on city.street=streets.id
WHERE division=2136 AND status=1 where street.id is null.

Merge three tables into one table, EXCEL POWERQUERY

Or with SQL, whatever needs.
I have three sheets(North, South, West) inside each table with columns DATE, REP, UNIT,NORTH(south west). I have data in rows like, 7/11/16, SMITH,FBI,$38.50 in sheet NOrth, and then in sheet West, 7/11/16,SMITH,FBI,$5.00. How to union or merge this kind of data? I tried with:
SELECT *from North$
Unon all
SELECT *from South$
Union all
Select *from West$
But, i only get append data. Those three tables have 47rows, i want those same rows in North and West to be joined in one row in new table!
Can i upload workbook? and how?
I would use the Power Query Merge command to join the rows from North and West. You haven't indicated what columns to join on but I'm sure you'll figure it out. The Merge popup window is quite helpful.
You haven't said what your requirements are for South - possibly a 2nd Merge?

mysql - Database organization for representing location

I need to create database which will contain the following entities in country:
Regions
Municipalities
Now comes the hard part for me:
Municipality can also be city, where city can (but not always) have its own municipalities.
Municipality can have several cities.
Municipality in both cases contains several settlements
I wanted to create several tables, for example:
regions (region_id, region_name)
municipalities (municipality_id, municipality_name, region_id).
cities (city_id, city_name, region_id)
city_municipalities (city_municipality_id, city_municipality_name, city_id)
settlements (settlement_id, settlement_name, municipality_id, city_municipality_id)
i think that here i make problem. There would be a lot of NULLs for municipality_id or city_municipality_id, since i cant have both field filled.
But right now it comes to my mind to have two tables organized like this:
geo_entity(geo_id, geo_name, geo_type_id, parent)
geo_entities_type (geo_type_id, geo_type_name)
this table will contain definitions of what geo_entity entry is, like:
1 Region
2 Municipality
3 City
4 City Municipality
5 Settlement
What should i stick to? Do you have a better approach and which one?

MYSQL Postcode lookup

I have a MYSQL table with the following fields postcode, town, county. Based on the postcode number I want to do a lookup to get the closest matching town and county.
The postcodes are essentially postcode districts, so for example:
AL1 = Saint Albans, Hertfordshire
AL10 = Hatfield, Hertfordshire
In the example above lets say I'm querying AL10 I want to retrieve the closest match, so in this case it would be AL10 = Hatfield, Hertfordshire. But if AL10 didn't exist I want to retrieve AL1.
I have got around this by first querying for a match on AL10 then if that failed AL1, then if that failed AL.
But this seems like a very inefficient way to achieve this seeing as I'm querying the table potentially 3 times. Is there a better and more efficient way to achieve the same goal?