1

It is tricky to explain, so lets start with an example:

I have a sqlite table containing multiple fields (id, language, title, etc.)

For one single title, they maybe several languages.

id --  language  --    title          --   publication  --  etc.
----------------------------------------------------------------------
1  --    Eng     --  Les misérables   --      1968      --  ...
2  --    Fr      --  Les misérables   --      1985      --  ...
3  --    Fr      --  Les misérables   --      2001      --  ...
4  --    Eng     --  Brave new world  --      1975      --  ...
5  --    Eng     --  Brave new world  --      1999      --  ...
6  --    Fr      --  Brave new world  --      1999      --  ...

My problem:

I would like in only one SELECT request to get the first result in English and in French.

SELECT (id WHERE language='Eng') AS id1, (id WHERE language='Fr') AS id2 FROM myTable GROUP BY title

To continue the example the request would give as a result

// this is javascript but the problem is about the sql part
results.rows.item(1).id1 = 1
results.rows.item(1).id2 = 2

results.rows.item(2).id1 = 4
results.rows.item(2).id2 = 6

etc.

Of course the syntax above is wrong, but is it possible to do in SQL?

4

2 回答 2

4

让我们从一个正确的语法示例开始:

SELECT title
FROM myTable
GROUP BY title

在这里,您有按标题分组的表myTable的所有行,例如

| title | id      | language            | ...
| 'Foo' | [1,4,6] | ['en', 'ger', 'fr'] | ...
| 'Bar' | [2,5]   | ['en', 'it']        | ...
...

[] 中的值表示由GROUP BY生成的组。

如果您只对 id 感兴趣,可以对列 id 应用 group_concat 操作

SELECT title, group_concat(id)
FROM myTable
GROUP BY title

这将为每个标题提供一个以逗号分隔的 id 列表的列(请参阅http://www.sqlite.org/lang_aggfunc.htmlSELECT ... group_concat(language)如果您需要这两种信息,您还可以在客户端上重新组装 id 和语言。

于 2013-07-01T08:16:00.283 回答
0

尝试 :

 SELECT
     (SELECT id From myTable WHERE language='Eng') AS id1, 
     (SELECT id From myTable WHERE language='Fr') AS id2,
     title
     FROM myTable GROUP BY title

您可以在子查询返回确切的 1 值时执行此操作,否则您会出错。

于 2013-07-01T08:09:00.183 回答