请帮我设计一个sql查询来解决这个问题
对于特定名称,我想获取居住在三个或更多城市的其他名称被这个人住过。
For a particular name i want to fetch other names who have lived in three or more cities lived by this person.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这就是你应该做的:
重新设计你的数据库以拥有一个城市表:
和一个用户表:
以及一个 user_city 表:
仅此一项就可以消除每个用户 10 个城市的限制。
查找用户居住的城市:
现在如何从该列表中查找居住在 3 个或更多城市的用户?
一种方法是从每个用户居住的列表中计算城市的数量,例如:
我没有真正测试过这个,但它应该可以工作。
您还必须弄清楚如何为这些表建立索引。
this is what you should do:
redesign your database to have a city table:
and a user table:
and a user_city table:
that alone will eliminate the limit of 10 cities per user.
to find the cities lived in by a user:
now how you would find users that lives in 3 or more cities from that list?
one way to do it would be to count the number of cities from the list each user lived in, something like:
I didn't really test this, but it should work.
you will also have to figure out how to index those tables.
您需要使用
binomial(10,3)^2
OR 条件来执行查询。那是 14 400。你不想这样做。You'd need
binomial(10,3)^2
OR conditions to do your query. Thats 14 400. You do not want to do that.您需要重新设计您的表,而不是
name 、 city1 、 city2 、 city3 、city4 、 city5 、city6 、 city7 、 city8 、 city9 city10 ,
它应该更像
是接受 TomTom 的建议并了解数据规范化!
You need to redesign your table instaed of
name , city1 , city2 , city3 ,city4 , city5 ,city6 , city7 , city8 , city9 city10
it should be more like
and take TomTom's advice and learn about data normalization!
尊重您不重新设计数据库的请求
我未经尝试的想法,现在无法测试
通过联合
select name, c1 创建视图(名称,城市)
、选择名称、c2
等...然后:
Respecting your request to not redesign the database
My untried idea, no way to test it right now
Make a view (name, city) by unioning
select name, c1
,select name, c2
etc...Then:
是的。
您可以将表格发送回设计者,并附上评论以了解如何设计表格。第一范式,规范化。
一旦表遵循 SQL 规则,查询就非常容易。
Yeah.
You send the table back to whoever designed it with a comment to learn hwo to design tables. First normal form, normalization.
Once the table follows SQL rules, the query is pretty easy.
尝试这样的操作:
我没有 mysql,所以这里使用 SQL Server 运行:
输出:
Try something like this:
I don't have mysql, so here it is running using SQL Server:
OUTPUT:
您需要标准化您的数据库。
执行此操作,您将获得
“名称”、“城市”列(可选的“城市顺序”)。
之后,您需要找到一种方法将这些结果组合成您需要的结果。为此,您需要了解连接、计数和分组依据。
You need to normalize your database.
Doing that you will get the columns
Name, City (optionally CityOrder).
After that you will need to find a way to combine these results into what you need. Doing this you'll need to understand Join, Count and Group by.
试试这个:
<表>人
<字段>人员 ID、人员名称 |
<表>城市
<字段>城市 ID、城市名称 |
<表>曾住过
<字段> LivedInId、PersonId、CityId
从逻辑上讲,您将为每个场景执行以下操作:
查找居住在最大数量的不同城市的人:
制作 PersonId 的列表(所有人)
迭代并计算每个人居住的城市数量
查找有人居住的最多城市
查找与拥有最多城市的 personId 相关的人名
查找居住在 3 个或更多城市的所有人员作为给定人员
我们称这个人为鲍勃
列出 Bob 居住过的所有城市(CityId)。
制作一个包含 personId 和常见城市的列表(可能是 Java 中的 HashMap)
迭代 LivedIn 表并更新常见城市的数量
查找计数大于 3 的所有人员
我会结合使用 Java 和 SQL 来完成此操作,但我对这两者都不太擅长,因此无法在此处为您提供代码,而不必查看很多东西向上。
Try this:
< table > Person
< fields > PersonId, PersonName |
< table > City
< fields > CityId, CityName |
< table > LivedIn
< fields > LivedInId, PersonId, CityId
Logically you would do the following things for each scenario:
Find the person who has lived in the maximum number of different cities:
Make a list of the PersonId's (all people)
Iterate over that and count the number of cities each person lived in
Find the maximum cities lived in by anyone person
Find the person name related to the personId that had the max cities
Find all people that lived in 3 or more cities as a give person
Let's call the person Bob
Make a list of all cities (the CityIds) that Bob lived in.
Make a list which includes personId, and common cities (maybe a HashMap in Java)
Iterate over the LivedIn table and update the count of how many cities are common
Find all the people who have a count greater than 3
I would do this with a combination of Java and SQL but I am not that good with either so can't give you the code here without having to look a lot of stuff up.
将此数据分为三个表以提供更灵活的多对多关系。
person
用于存储姓名的表用于存储城市的
city
表person_city
将两者关联起来(多对多)要检索曾居住在 navin 拥有的 3 个或更多城市的其他人:
Breaking this data out into three tables to provide a more flexible many-to-many relationship.
person
table to store namescity
table to store citiesperson_city
to relate the two (many to many)To retrieve other people who have lived in 3 or more cities that navin has: