在 EAV 中选择不同的值对
我正在开发一个用户数据库,其中配置文件数据已从简单的表更改为实体属性值表。
与以前一样,结构是这样的:
userid (int)
address 1 (varchar)
city (varchar)
country (varchar)
现在是这样的:
userid (int)
key (varchar)
value (varchar)
例如,
userid key value
150 city London
150 country UK
151 city New York
151 country USA
152 country Mexico
我需要获取城市/国家对的不同列表以及每个国家/地区的所有用户的计数:
city country count
London UK 18
New York USA 25
不能保证每个键值对都会存在每个用户,即可以有城市、国家或两者或两者都没有,以及任意数量的其他键值对。
对于旧结构来说这很简单,但我什至不知道如何开始,并且将不胜感激
I'm working on a user database where the profile data has been changed from a simple table into a Entity-Attribute-Value table.
Where as before the structure was along these lines:
userid (int)
address 1 (varchar)
city (varchar)
country (varchar)
It's now along these lines:
userid (int)
key (varchar)
value (varchar)
eg
userid key value
150 city London
150 country UK
151 city New York
151 country USA
152 country Mexico
I need to get a distinct list of city / country pairs and a count of all users for each country:
city country count
London UK 18
New York USA 25
There is no guarantee each key value pair will exist for each user, i.e there could be city, or country or both or neither as well as any number of other key values pairs.
This was straightforward with the old structure, but I can't even think how to begin on this, and would be grateful for some pointers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您最好的解决方案是返回到传统表,因为 EAV 使大多数查询变得比应有的困难得多 - 在这里见证您的问题。您将进行自连接,直到您厌倦为止,重新构建允许您执行合理查询的表结构。
每个用户 ID 的城市和国家/地区:
因此,您最终会得到:
如果某人可能有两个城市或两个国家/地区记录,这将为您提供一个笛卡尔积,其中该用户的行数与数字的乘积一样多该用户的城市和国家记录。
这相当有意且有意识地忽略了拥有城市但没有国家或国家但没有城市的用户(更不用说那些既没有国家又没有城市的用户了)。扩展解决方案来处理这些问题只是有点痛苦 - 我认为您最终会得到一个 3 路 UNION,尽管您可能能够设计出具有多个左外连接的东西。但事实上,数据可以输入 EAV 系统而无需必要的限制,以确保用户有一个城市和一个国家,这只是拒绝 EAV 的众多原因之一。
我很抱歉你把这个强加给你。我建议您查看 http://careers.stackoverflow.com/ 作为摆脱痛苦的方法,因为这只是开始它。
与没有城市或国家或两者的用户打交道。我认为这或多或少会做到这一点:
只要该用户没有多个城市或国家记录,这应该为每个用户提供一条记录。
a
扫描为您提供 EAV 表中存在的唯一用户 ID 的列表;两个外部联接为您提供每个此类用户 ID 的相应城市和相应国家/地区,如果给定用户 ID 没有城市记录或国家记录(或两者),则会生成空值。Your best solution is to go back to the traditional table because EAV makes most querying much harder than it should be - witness your problems here. You're going to be doing self-joins until you're sick of them, remanufacturing the table structure that allows you to perform sensible queries.
Cities and countries for each user ID:
So, you end up with:
If there's a chance that someone might have two city or two country records, this will give you a Cartesian product with as many rows for that user as the product of the number of city and country records for that user.
This quite deliberately and consciously ignores users who have a city and no country or a country and no city (let alone those who have neither). Extending the solution to deal with those is only modestly painful - you end up with a 3-way UNION, I think, though you might be able to devise something with multiple left outer joins. But the fact that data can be entered into an EAV system without the necessary constraints to ensure that there is a city and a country for a user is simply one of the many reasons for rejecting EAV.
I'm sorry you had this foisted on you. I recommend looking at http://careers.stackoverflow.com/ as a way out of your pain, for this is only the beginning of it.
Dealing with users without either city or country or both. I think this will more or less do it:
This should give you one record per user as long as there are no multiple city or country records for that user. The
a
scan gives you the list of unique user IDs that exist in the EAV table; the two outer joins give you the corresponding city or cities and corresponding country or countries for each such user ID, with nulls being generated if there is no city record or country record (or both) for the given user ID.回复:我需要获取城市/国家对的不同列表
re: I need to get a distinct list of city / country pairs