是否有规范化列=行的表的快捷方式?
假设您有 mySQL 表描述是否可以混合两种物质,
Product A B C
---------------------
A y n y
B n y y
C y y y
第一步是将其转换为:
P1 P2 ?
-----------
A A y
A B n
A C y
B A y
B B y
B C n
C A y
C B n
C C y
但是您有重复的信息。 (例如,如果 A 可以与 B 混合,那么 B 可以与 A 混合),因此,您可以删除几行来获取
P1 P2 ?
-----------
A A y
A B n
A C y
B B y
B C n
C C y
虽然最后一步对于小表来说非常容易,但在较大的表上手动执行将永远需要时间。如何自动删除具有重复含义但内容不相同的行?
谢谢,我希望我的问题有意义,因为我仍在学习数据库
Suppose you had the mySQL table describing if you can mix two substances
Product A B C
---------------------
A y n y
B n y y
C y y y
The first step would be to transform it like
P1 P2 ?
-----------
A A y
A B n
A C y
B A y
B B y
B C n
C A y
C B n
C C y
But then you have duplicate information. (eg. If A can mix with B, then B can mix with A), so, you can remove several rows to get
P1 P2 ?
-----------
A A y
A B n
A C y
B B y
B C n
C C y
While the last step was pretty easy with a small table, doing it manually would take forever on a larger table. How would one go about automating the removal of rows with duplicate MEANING, but not identical content?
Thanks, I hope my question makes sense as I am still learning databases
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果可以安全地假设您从所有关系都加倍开始,例如,
如果
A B
位于表中,则B A
保证位于表中。然后您所要做的就是删除 P2 < 的所有行。 P1;
如果不是这种情况,您可以通过遍历表并插入所有重复行(如果它们尚不存在),然后运行它来实现这种情况。
If it's safe to assume that you're starting with all relationships doubled up, e.g.
If
A B
is in the table, thenB A
is guaranteed to be in the table.Then all you have to do is remove all rows where P2 < P1;
If this isn't the case, you can make it the case by going through the table and inserting all the duplicate rows if they don't already exist, then running this.
我认为在您的情况下没有必要,但作为一项智力练习,您可以以 Jamie Wong 的解决方案为基础,并防止使用 EXISTS 子句删除非重复列。像这样的事情:
它几乎只是在删除任何内容之前确保存在重复项。
(我的 MySQL 语法可能有点不对;已经有一段时间了。)
I don't think it's necessary in your situation, but as an intellectual exercise, you could build on Jamie Wong's solution and prevent non-duplicated columns from being removed with an EXISTS clause. Something like this:
It pretty much just makes sure that there's a duplicate before deleting anything.
(My MySQL syntax might be a little off; it's been a while.)
第 1 步(正如您已经完成的那样):转换为 Table2
第 2 步:重新排序列,选择不同
我不是 mySQL 人员,因此您可能需要检查 if/then 语法,但这在概念上无论如何似乎都可以。
Step 1 (as you've already done): Transform to Table2
Step 2: ReOrder Columns, Select Distinct
I'm not a mySQL guy, so you might need to check the if/then syntax, but this seems conceptually ok anyway.