0

我正在尝试创建一个复杂的 SQL 查询(至少对我而言),但真的不知道从哪里开始。

基本上,我有一个角色对象,每个角色都可以由几个部分组成。例如:

字符表:

id | character
--------------
1  | 你
2  | 是
3  | 有

character_parts表:

id | character_id | part_id
---------------------------
1  | 1            | 4
2  | 1            | 9
3  | 1            | 5
4  | 2            | 2
5  | 2            | 34
6  | 2            | 43
7  | 3            | 21
8  | 3            | 16
9  | 3            | 41
10 | 3            | 43

因此,我知道:

Character 1 is made of parts 4, 9, 5
Character 2 is made of parts 2, 34, 43
Character 3 is made of parts 21, 16, 41, 43

现在我想做的是选择以指定部分结尾的所有字符。

例如,如果我选择所有以“16、41、43”结尾的字符,我将得到字符 3。如果我选​​择所有以“43”结尾的字符,我将得到字符 3 和 4。

我假设我需要使用子查询构建一些查询,但不确定如何开始,或者是否可以完成。到目前为止,我只是选择了包含所需部件 ID 的所有内容,然后以编程方式进行比较,但这太慢了,因为我选择的方式超出了需要。

有什么建议吗?

4

4 回答 4

0

查询可能是这样的。

select * from character where id in (select character_id from character_parts where character_id = 'required no' AND character_id = 'required no' AND character_id = 'required no')
//required number is the part_id you want to specify.
于 2012-04-29T08:48:29.850 回答
0

添加另一列,from_end从末尾开始计算,例如:

from_end | id | character_id | part_id
--------------------------------------
      2  | 1  | 1            | 4
      1  | 2  | 1            | 9
      0  | 3  | 1            | 5
      2  | 4  | 2            | 2
      1  | 5  | 2            | 34
      0  | 6  | 2            | 43
      3  | 7  | 3            | 21
      2  | 8  | 3            | 16
      1  | 9  | 3            | 41
      0  | 10 | 3            | 43

然后你可以这样做:

SELECT p0.character_id
  FROM character_parts AS p0
  JOIN character_parts AS p1 USING (character_id)
  JOIN character_parts AS p2 USING (character_id)
 WHERE p0.from_end = 0 AND p1.from_end = 1 AND p2.from_end = 2
   AND p0.part_id = 43 AND p1.part_id = 41 AND p2.part_id = 16
于 2012-04-29T08:59:33.977 回答
0

你可以试试group_concat功能: http ://www.sqlite.org/lang_aggfunc.html

SELECT group_concat(part_id) FROM character_parts WHERE character_id=1

查询应该返回4,9,5

问题是 by 使用的顺序group_concat是任意的:

Sqlite group_concat 排序

因此,假设您有一个position定义部件顺序的字段,我们可以像这样更新查询:

SELECT group_concat(part_id) FROM (SELECT part_id FROM character_parts WHERE character_id=1 ORDER BY position ASC)

查询现在将4,9,5完全按照定义的顺序返回零件。

现在我们有了这个值,我们可以像普通字符串一样搜索它。如果我们想查找所有以某个字符串结尾的值,我们可以使用LIKE运算符。

最后查询会是这样的:

SELECT character_id, parts_concat FROM (
    SELECT character_id, group_concat(part_id) FROM (
        SELECT character_id, part_id FROM character_parts WHERE ORDER BY position ASC
    ) GROUP BY character_id
) parts
WHERE parts_concat LIKE '%,9,5'
于 2012-04-29T09:06:48.123 回答
0
SELECT  c.character
FROM    character_parts cp
JOIN    character c
ON      c.id = cp.character_id
WHERE   cp.part_id = 43
        AND cp.id =
        (
        SELECT  id
        FROM    character_parts cpo
        WHERE   cpo.character_id = cp.character_id
        ORDER BY
                id DESC
        )

创建以下索引:

character_parts (part_id, character_id, id)
character_parts (character_id, id)

为了快速工作

于 2012-04-29T09:17:04.543 回答