MySQL在字符串上相交?

时间:2013-07-15 17:27:53

标签: mysql database intersect

我有两张桌子:

Quest
- (int) id 
- (text) characters

User
- (int) id
- (text) characters

参赛作品如下:

任务

id | characters
1  | abcdefgh
2  | mkorti
3  | afoxi
4  | bac

用户

id | characters
1  | abcd

现在我想为用户选择最简单的任务。最简单的任务是quest.characters和user.characters的最交叉点。所以在这个例子中,列表看起来像这样(对于user.id = 1):

questid | easiness
4       | 100
1       | 50
3       | 40
2       | 0

简单性只表示匹配了多少百分比。 MySQL有可能像这样交叉列吗?性能如何?事实上我也有关系(任务 - >字符和用户 - >字符),但我想它不是很高效。因为有几千个任务,还有几千个字符。

更新#1

好的,关系仍然似乎要走了,好的。现在我的表格看起来像这样:

CREATE TABLE IF NOT EXISTS `quest` (
  `questid` int(10) unsigned NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`questid`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 ;

CREATE TABLE IF NOT EXISTS `questcharacters` (
  `questid` int(10) unsigned NOT NULL,
  `characterid` int(10) unsigned NOT NULL,
  PRIMARY KEY (`questid`,`characterid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


CREATE TABLE IF NOT EXISTS `single_character` (
  `characterid` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `single_char` varchar(10) NOT NULL,
  PRIMARY KEY (`characterid`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;


CREATE TABLE IF NOT EXISTS `user` (
  `userid` int(10) unsigned NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;


CREATE TABLE IF NOT EXISTS `usercharacters` (
  `userid` int(10) unsigned NOT NULL,
  `characterid` int(10) unsigned NOT NULL,
  PRIMARY KEY (`userid`,`characterid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

PS:不要奇怪为什么single_char将VARCHAR(10)作为数据类型,但我将使用多字节值,我不知道MySQL如何处理char(1)。所以我在那里很慷慨。

更新#2

我现在的查询是:

SELECT usercharacters.userid, questcharacters.questid
FROM `usercharacters`
LEFT OUTER JOIN questcharacters ON usercharacters.characterid = usercharacters.characterid
GROUP BY questcharacters.questid, usercharacters.userid;

但是如何计算容易/重叠字符?我必须在哪个字段中应用COUNT()?

更新#3

好吧,好像我使用了这个查询(使用子选择):

SELECT usercharacters.userid as uid, questcharacters.questid as qid, (SELECT COUNT(questcharacters.characterid) FROM questcharacters LEFT OUTER JOIN usercharacters ON questcharacters.characterid = usercharacters.characterid WHERE questcharacters.questid = qid) as questcount
FROM `usercharacters`
LEFT OUTER JOIN questcharacters ON usercharacters.characterid = usercharacters.characterid
GROUP BY questcharacters.questid, usercharacters.userid;

更新#4

SELECT usercharacters.userid as uid, questcharacters.questid as qid, (SELECT COUNT(questcharacters.characterid) FROM questcharacters LEFT OUTER JOIN usercharacters ON questcharacters.characterid = usercharacters.characterid WHERE questcharacters.questid = qid) as user_knows, (SELECT COUNT(questcharacters.characterid) FROM questcharacters WHERE questcharacters.questid = qid) as total_characters
FROM `usercharacters`
LEFT OUTER JOIN questcharacters ON usercharacters.characterid = usercharacters.characterid
GROUP BY questcharacters.questid, usercharacters.userid
ORDER BY total_characters / user_knows DESC;

现在只缺少一件事:选择简单。 (如在ORDER BY子句中)。谁知道怎么做?

2 个答案:

答案 0 :(得分:0)

如果您实际拥有questcharacterusercharacters表,那么这是最好的方法:

SELECT uc.id AS userid, 
       qc.id AS qcid, 
       COUNT(*) AS NumCharacters,
       COUNT(qc.char) AS Nummatches,
       COUNT(qc.char) / count(*) AS Easiness
FROM UserCharacters uc 
   LEFT OUTER JOIN QuestCharacters qc ON uc.char = qc.char
WHERE uc.id = 1
   GROUP BY uc.id, qc.id
   ORDER BY easiness DESC
LIMIT 1

如果你只将它们作为字符串 - 那么SQL并不漂亮。你必须进行交叉连接和大量的字符串操作。最好的方法是以关系数据库的形式(每个列表元素一行)更加规范化,而不是将列表嵌入字符串中。

答案 1 :(得分:0)

所以这是我最后的工作解决方案:

SELECT usercharacters.userid                  AS uid, 
       questcharacters.questid                AS qid, 
       (SELECT Count(questcharacters.characterid) 
        FROM   questcharacters 
               LEFT OUTER JOIN usercharacters 
                            ON questcharacters.characterid = 
                               usercharacters.characterid 
        WHERE  questcharacters.questid = qid) AS user_knows, 
       (SELECT Count(questcharacters.characterid) 
        FROM   questcharacters 
        WHERE  questcharacters.questid = qid) AS total_characters, 
       (SELECT ( Count(questcharacters.characterid) / (SELECT 
                         Count(questcharacters.characterid) 
                                                       FROM   questcharacters 
                                                       WHERE 
                 questcharacters.questid = qid) ) 
        FROM   questcharacters 
               LEFT OUTER JOIN usercharacters 
                            ON questcharacters.characterid = 
                               usercharacters.characterid 
        WHERE  questcharacters.questid = qid) AS ratio 
FROM   `usercharacters` 
       LEFT OUTER JOIN questcharacters 
                    ON usercharacters.characterid = usercharacters.characterid 
GROUP  BY questcharacters.questid, 
          usercharacters.userid 
ORDER  BY ratio DESC; 

我真的需要那么多次选择吗?