帮助 mysql 查询或数据库设计来帮助我实现这一目标

发布于 2024-10-23 22:05:33 字数 860 浏览 1 评论 0原文

我一直是该网站的长期读者,但以前从未发过帖子,希望这里有人能提供帮助。我正在开发一个多用户 Web 应用程序,该应用程序允许用户将潜在客户信息捕获到我们的数据库中。用户将在应用程序内获得默认字段,例如分组为数据组的姓名和电子邮件。

但是,我希望允许用户创建任意数量的自定义字段,并允许他们选择这些自定义字段属于哪个数据组。我正在努力找出实现这一目标的最佳方法。

我的数据中目前有 3 个表,如下所示:

datagroups

group_id   group_name    order
1          test group 1    2
2          test group 2    1


datafields

field_id      field_name    group
   1           field 1        2
   2           field 2        1
   3           field 3        2


customdatafields

custom_field_id     field_name      group
      1               custom 1       1
      2               custom 2       2

我真的很困惑如何在这 3 个表中创建查询,以便我可以在显示后生成视图:

test group 2
- field 1
- field 3
- custom 2

test group 1
- field 2
- custom 1

我需要记住的一件事是我可能允许用户创建自定义数据组,这也需要考虑在内。

对此的任何意见将不胜感激。

谢谢

I have been a long time reader of site but havenever posted before and am hoping someone here can help. I am working on a multi-user web application which allows users to capture prospect information into our database. The users will be given default fields inside the app, such as name and email which are grouped into data groups.

However i want to allow the users to create as many custom fields as they wish and also allow them to choose which data group these custom fields belong in group. I am struggling to figure out the best way to achieve this.

I have currently have 3 tables in my data which are as follows:

datagroups

group_id   group_name    order
1          test group 1    2
2          test group 2    1


datafields

field_id      field_name    group
   1           field 1        2
   2           field 2        1
   3           field 3        2


customdatafields

custom_field_id     field_name      group
      1               custom 1       1
      2               custom 2       2

I am really puzzled on how I would create a query across the 3 tables so that I can produce a view following display:

test group 2
- field 1
- field 3
- custom 2

test group 1
- field 2
- custom 1

One thing I need to keep in mind is that I may allow the users to create custom data groups also that that needs to be factored into this.

Any input on this would be much appreciated.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

べ繥欢鉨o。 2024-10-30 22:05:34

好吧,您所描述的输出不是表格形式的...所以不是直接从 sql 语句中得到的输出。

但是,您应该能够通过使用 UNION 来获取列表,可能是这样的:

select g.group_id, g.group_name, f.field_name
from datagroups g, 
    (select field_name, field_id, group from datafields 
     union
     select custom_field_name, field_id, group from customdatafields 
    ) f
where f.group = g.group_id
order by g.order

well, your described output is not tabular... so not directly what you will get form a sql statement.

however, you should be able to get listings by using UNION maybe something like this:

select g.group_id, g.group_name, f.field_name
from datagroups g, 
    (select field_name, field_id, group from datafields 
     union
     select custom_field_name, field_id, group from customdatafields 
    ) f
where f.group = g.group_id
order by g.order
凹づ凸ル 2024-10-30 22:05:33

但是我想允许用户
创建尽可能多的自定义字段
希望并允许他们选择
这些自定义字段对哪些数据进行分组
属于组。

您刚刚让每个临时用户成为您的数据库设计师。

这听起来是个好主意吗?

任何应该表格中的行最终都会成为单列中的逗号分隔字符串。数据迟早会变成这样。

Home: 123-456-7890, Work: 123-456-0987, Cell: 123-454-5678
H: 123-454-6453, W: 123-432-5746, 800: 1-800-555-1212
234-345-4567, 234-345-6785
323-123-4567 Don't call before 10:00 am

人们为什么要这么做?因为普通人不知道如何设计数据库。他们会做最权宜的事情,就是现在把数据随便放,然后再想办法处理。

哎呀,SO 上的大多数程序员都不知道如何设计数据库。只需阅读一周左右的[数据库]问题即可。 (包括这个版本的十几个变体。)

不想为此使用 MySQL 或任何 SQL dbms。如果您一开始就不能做好建模工作,那么 SQL 数据库管理系统只会让您提前死去。

However i want to allow the users to
create as many custom fields as they
wish and also allow them to choose
which data group these custom fields
belong in group.

You've just made every casual user your database designer.

Does that sound like a good idea?

Anything that should be a row in a table will end up being a comma separated string in a single column. Data will end up looking like this, sooner or later.

Home: 123-456-7890, Work: 123-456-0987, Cell: 123-454-5678
H: 123-454-6453, W: 123-432-5746, 800: 1-800-555-1212
234-345-4567, 234-345-6785
323-123-4567 Don't call before 10:00 am

Why would people do that? Because ordinary people don't know how to design databases. They'll do the most expedient thing, which is just put data in any which way now, and figure out how to deal with it later.

Heck, most of the programmers on SO don't know how to design databases. Just read [database] questions for a week or so. (Including a dozen variants of this very one.)

You do not want to use MySQL or any SQL dbms for this. If you can't do a proper job of modeling to start with, a SQL dbms will just make you die before your time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文