【问题标题】:Picking certain number of groups of data in SQL on the fly在 SQL 中动态选择一定数量的数据组
【发布时间】:2014-07-10 23:31:30
【问题描述】:

我有两张桌子。右边是查找表,左边是我有问题的表。 num 是主键,WordID 是引用 num 的外键。 Pri 是表示单词优先级的数字(编号最小的单词在前),ID 表示单词组。对于我的例子,我有以下......

第 1 组:学校奶油球 第三组:奶油球店 第22组:门房 第五组:球 第 7 组:汽车

这就是问题所在。

我希望我的应用程序让我有机会添加一个新词集,该词集可以包含新词和/或现有词,但一次只能包含一个词。

例如,如果我首先选择 cream 这个词,两个现有的组中都有这个词,但我不知道如何确定这些词是否有额外的词以及有多少额外的词。

如果单词组相同且顺序相同,我正在尝试弄清楚如何进行此操作,而不必每次都插入新记录。

我正在尝试以编程方式使这种设计尽可能高效,而不会浪费磁盘空间。

所以基本上我想做的是创建一组单词并在添加或删除部分单词时更新数据库,这样如果服务器崩溃,至少会保存部分数据。

有什么想法吗?


更新:我添加了一个新示例以及代码以更好地说明我的问题。

drop table if exists words;drop table if exists word;drop table if exists name;
create table word(ID Int primary key not null auto_increment, Word varchar(255));
create table name(ID int, Obj int);
create table words(Seq  Int primary key not null auto_increment,NameID Int,WordID Int,foreign key (WordID) references word(ID));

insert into word(Word) values("great"),("pink"),("job"),("ribbon"),("nice"),("red"),("tie"),("bow"); # make up words

# add group 1 = great job (words 1 and 3 respectively).
insert into words(NameID,WordID) values(1,1),(1,3);
# add group 2 = nice job (words 5 and 3 respectively).
insert into words(NameID,WordID) values(2,5),(2,3);
# add group 3 = nice tie (words 5 and 7 respectively).
insert into words(NameID,WordID) values(3,5),(3,7);
# add group 4 = bow tie (words 8 and 7 respectively).
insert into words(NameID,WordID) values(4,8),(4,7);
# add group 5 = nice bow tie (words 5, 8 and 7 respectively).
insert into words(NameID,WordID) values(5,5),(5,8),(5,7);
# add group 6 = nice bow (words 5 and 8 respectively).
insert into words(NameID,WordID) values(6,5),(6,8);

# define three wordsets for object #1 and three for object #2 and two for object #3, and a brand new set for object #4.
insert into name(ID,Obj) values(1,1),(2,1),(3,1),(1,2),(4,2),(5,2),(6,3),(5,3),(7,4);

#object 1 has these sets: great job, nice job, nice tie
#object 2 has these sets: great job, bow tie, nice bow tie
#object 3 has these sets: nice bow tie, nice bow
#object 4 has word set 7 which we are about to define.

# At this point, data is ok. Problem starts when new data begins
# So I look for a key (NameID) that hasnt been used yet. in this case, its 7.


# Lets say my application displays each word from the lookup table in its own button on the screen where I can 
# select whatever word I want to add, and on each selection, the button lights up and the database updates.
# if i click the same word again, it is removed and the database is updated.

# so I want to add the word "nice" to set 7 like so...

insert into words(NameID,WordID) values(7,5);

# I can keep that wordset as-is because there is no other instance of that word set. (the set containing only the word "nice"). 
# Lets say I click the "bow" button to add the word "bow" after "nice" to set 7 as well.

insert into words(NameID,WordID) values(7,8);

# now Heres the problem. I have just created the word set nice bow. This also matches word group 6.
# If I keep telling SQL to define objects as the words "nice bow" the way I do it,
# I will have duplicate results, and waste disk space. I will add a few more "nice bow"s to show.

insert into words(NameID,WordID) values(8,5),(8,8);
insert into words(NameID,WordID) values(9,5),(9,8);
insert into words(NameID,WordID) values(10,5),(10,8);

# Running "select * from words" will show the table with the last 5 sets of words being exactly the same. The seq value is only used 
# to determine order. the lower the sequence number, the more closer to the beginning the word is for that group.

不知何故,我想将最后四个条目转换为一个,以消除应用程序中每次按钮单击时的冗余。

有人知道我做错了什么吗?我知道我需要使用 SELECT,我想我可能需要使用 COUNT、HAVING、GROUP 等等,但我很困惑

【问题讨论】:

  • 到目前为止你做了什么?
  • group by 和 having 的组合。我可以使用select * from ObjWords group by WordID having count(*)=3; 获得字数。 Objwords 是我的问题中显示的 3 列表。
  • 以下是我要构建一组单词的步骤... 1. 获取随机词组 ID 和随机词。 (我会说 10 和 20) 2. 检查仅包含单词 20 的其他组。如果存在,则将组 ID 更改为仅包含单词 20 的组的 ID。否则添加一条新记录。
  • 我的下一步是然后使用组 id 并添加另一个单词并记住最后一个单词,但在将另一个单词添加到组之前,查看其他组是否包含相同的两个数字(代表相同的两个单词),如果不存在这样的组,则添加一个条目。否则,将组 ID 更改为包含这两个单词的组的 ID。
  • 如果我想添加第三个和第四个单词等,同样的事情会再次发生。此外,如果我想从列表中删除单词,也会发生类似的事情(除了我删除记录而不是添加记录)。

标签: mysql sql grouping


【解决方案1】:

我对你冗长的问题有点不清楚。尽管如此,这仍然是您可以在 MySQL 中实现随机选择的方式。

SELECT A, B, C, D FROM TABLE 
ORDER BY RAND()

OR

SELECT A, B, C, D FROM TABLE,
(SELECT RAND()*A AS A FROM TABLE) AS TMP
WHERE 
TABLE.A = TMP.A

我建议您尝试这两种方法。我没有尝试过任何一个。尝试在您的场景中实现这一点。

【讨论】:

  • 我试过这个:select * from words where wordID in (5,8) group by NameID having count(NameID)=2 看看我是否可以列出所有只有上面红色圈出的数据集的字段,但它也显示了单词集编号 5。我知道单词 ID 5 和 8 在第 5 组中,但它还有另一个我不想要的字号。
猜你喜欢
  • 1970-01-01
  • 2014-05-17
  • 2012-02-21
  • 2012-02-14
  • 2012-05-01
  • 1970-01-01
  • 2018-03-20
  • 2016-09-01
  • 2020-06-06
相关资源
最近更新 更多