3

我无法解决这个问题好几个小时。
这是我的桌子

t1:

–––––––––––––––––––––––––––––––––––
| id | text   |  lang | transl_id |
–––––––––––––––––––––––––––––––––––
| 1  | first  |  en   |  222      |
–––––––––––––––––––––––––––––––––––
| 2  | second |  de   |  222      |
–––––––––––––––––––––––––––––––––––
| 3  |   jkj  |  de   |  234      |
–––––––––––––––––––––––––––––––––––
| 4  |  89080 |  de   |  235      |
–––––––––––––––––––––––––––––––––––

这是我的查询:

SELECT
    transl_id AS property,
    (SELECT text FROM t1 WHERE lang='en') AS value1,
    (SELECT text FROM t1 WHERE lang='de') AS value2,

FROM t1

它返回下表:

–––––––––––––––––––––––––––––––––––
| property  |  value1  |  value2  |
–––––––––––––––––––––––––––––––––––
|    222    |  first   |          |
–––––––––––––––––––––––––––––––––––
|    222    |          |  second  |
–––––––––––––––––––––––––––––––––––
|    234    |  jkj     |          |
–––––––––––––––––––––––––––––––––––
|    235    |  89080   |          |
–––––––––––––––––––––––––––––––––––

每行都有一个value1value2,从不兼有。有没有办法对结果进行分组,以便property字段值相等的行在一行中?我的意思是这样的:

–––––––––––––––––––––––––––––––––––
| property  |  value1  |  value2  |
–––––––––––––––––––––––––––––––––––
|    222    |  first   |  second  |
–––––––––––––––––––––––––––––––––––
...
4

2 回答 2

3

试试这个查询:

SELECT
    property,
    max(value1) as Value1,
    max(value2) as Value2
FROM 
(
SELECT transl_id AS property,
    CASE when lang = 'en' then text else null end as value1,
    CASE when lang = 'de' then text else null end as value2
FROM t1
) t
GROUP BY property

看到这个 SQLFiddle

尝试在表中添加更多值并在此 SQLFiddle 中获得所需的结果。

于 2012-09-08T13:57:00.043 回答
0

也许是这样的:

select t1.property, t1.value1, t2.value2 from
( select property, value1 from table1 where value1 is not null ) t1,
( select property, value2 from table1 where value2 is not null ) t2
where t1.property = t2.property
于 2012-09-08T13:16:25.637 回答