SQL - 在Query中连接同一个表中的列

时间:2011-09-11 20:29:47

标签: sql join publishing openoffice-base

解决了!请看下面的答案!

在我解释我的问题之前,我想为那些觉得这个问题太长的人道歉,但我觉得我必须提供一些细节,以使事情更清晰。虽然,问题很容易理解,但实施并不是那么简单。

我有3张桌子。

Hata和Icon包含我想要与包含文本的Succes链接的图像

[畑]

id, INTEGER, AUTO_INCREMENT, PRIMARY_KEY
hata Image
idLang, VARCHAR(5)

[ICON]

id, INTEGER, AUTO_INCREMENT, PRIMARY_KEY
icon, IMAGE
idPhrase, INTEGER

[SUCCES]

id, INTEGER, AUTO_INCREMENT, PRIMARY_KEY
idPhrase, INTEGER
titre, VARCHAR(25)
desc, VARCHAR(125)
idLang, VARCHAR(5)

以下示例显示了Succes表的外观

+----+----------+-----------------+------------------+--------+
| id | idPhrase | titre           | desc             | idLang |
+----+----------+-----------------+------------------+--------+
|  1 |        1 | Hello           | Desc in English  | en-GB  |
+----+----------+-----------------+------------------+--------+
|  2 |        1 | Salut           | Desc in French   | fr-FR  |
+----+----------+-----------------+------------------+--------+
|  3 |        1 | 今日は           | Desc in Japanese | ja-JP  |
+----+----------+-----------------+------------------+--------+
|  4 |        2 | Goodbye         | Desc in English  | en-GB  |
+----+----------+-----------------+------------------+--------+
|  5 |        2 | Au revoir       | Desc in French   | fr-FR  |
+----+----------+-----------------+------------------+--------+
|  6 |        2 | またね            | Desc in Japanese | ja-JP  |
+----+----------+-----------------+------------------+--------+
|  7 |        3 | You're welcome  | Desc in English  | en-GB  |
+----+----------+-----------------+------------------+--------+
|  8 |        3 | Je vous en prie | Desc in French   | fr-FR  |
+----+----------+-----------------+------------------+--------+
|  9 |        3 | どういたしまして      | Desc in Japanese | ja-JP  |
+----+----------+-----------------+------------------+--------+
...

现在使用此 WHERE 条件

连接表格

Icons.idPhrase = Succes.idPhrase AND Hata.idLang = Succes.idLang

如果Succes表中没有任何具体内容,那么一切都会好的。

事实上,对于每个Icon,有3个句子,idPhrase链接它们,但在实际结果集中,我不知何故有冗余。

Icon1|FlagIcon1|TitreLang1|DescLang1
Icon1|FlagIcon2|TitreLang2|DescLang2
Icon1|FlagIcon3|TitreLang3|DescLang3
Icon2|FlagIcon1|TitreLang1|DescLang1
Icon2|FlagIcon2|TitreLang2|DescLang2
Icon2|FlagIcon3|TitreLang3|DescLang3
...

我想要实现的目标如下(只是第一行):

Icon1|FlagIcon1|TitreLang1|DescLang1|FlagIcon2|TitreLang2|DescLang2|FlagIcon3|TitreLang3|DescLang

Icon1|FlagIcon1|FlagIcon2|FlagIcon3|TitreLang1|DescLang1|TitreLang2|DescLang2|TitreLang3|DescLang3

甚至 Icon1|FlagIcon1|FlagIcon2|FlagIcon3|TitreLang1|TitreLang2|TitreLang3|DescLang1|DescLang2|DescLang3

换句话说,就像我一起加入了几个问题,例如

SELECT icon FROM Icon

加入

SELECT Hata.hata AS fEN, Succes.titre AS tEN, Succes.desc AS dEN
FROM Hata, Succes
WHERE Hata.idLang=Succes.idLang AND Succes.idLang='en-GB'

加入

SELECT Hata.hata AS fFR, Succes.titre AS tFR, Succes.desc AS dFR
FROM Hata, Succes
WHERE Hata.idLang=Succes.idLang AND Succes.idLang='fr-FR'

依旧...... 只是确保表格之间链接的问题(图标1和句子1)

这是另一个关于它应该(可能)如何的样本

+-------+-------+-------+-------+----------------+------------------+------------+-----------------+----------------+------------------+
| icon  | fEN   | fFR   | fJP   | tEN            | tFR              | tJA        | dEN             | dFR            | dJA              |
+-------+-------+-------+-------+----------------+------------------+------------+-----------------+----------------+------------------+
| <img> | <img> | <img> | <img> | Hello          | Salut            | 今日は      | Desc in English | Desc in French | Desc in Japanese |
+-------+-------+-------+-------+----------------+------------------+------------+-----------------+----------------+------------------+
| <img> | <img> | <img> | <img> | Goodbye        | Au revoir        | またね       | Desc in English | Desc in French | Desc in Japanese |
+-------+-------+-------+-------+----------------+------------------+------------+-----------------+----------------+------------------+
| <img> | <img> | <img> | <img> | You're welcome | Je vous en pries | どういたしまして | Desc in English | Desc in French | Desc in Japanese |
+-------+-------+-------+-------+----------------+------------------+------------+-----------------+----------------+------------------+
...

我已经浏览过SQL引用尝试很多东西,但它们似乎没有按照我的预期进行(CONCATENATE,UNION等...) 我也尝试了以下查询,但它给了我一条错误信息。

SELECT Icon.icon, Hata.hata AS fEN,Hata.hata AS fFR,Hata.hata AS fJA
    ,'FR'.'titre', 'FR'.'desc'
    ,'JA'.'titre', 'JA'.'desc'
    ,'UK'.'titre', 'UK'.'desc'
FROM Hata, Icon
LEFT JOIN Succes AS FR ON 'FR'.'idLang' = 'Hata'.'idLang' AND 'FR'.'idLang' = 'fr-FR'
LEFT JOIN Succes AS JA ON 'JA'.'idLang' = 'Hata'.'idLang' AND 'FR'.'idLang' = 'ja-JP'
LEFT JOIN Succes AS UK ON 'UK'.'idLang' = 'Hata'.'idLang' AND 'FR'.'idLang' = 'en-GB'

消息是

Statut SQL: HY000
Error Code: 1000

syntax error, unexpected $end, expecting BETWEEN or IN or SQL_TOKEN_LIKE

但根据我在StackOverflow上发现的样本,我的语法似乎很好。 我还必须指定我正在使用OpenOffice Base,我的目的是发布文档。也许有一些特定于OOo的东西,例如LEFT JOIN没有实现,但代码变色,所以我认为应该没问题。

感谢您的支持和帮助。

2 个答案:

答案 0 :(得分:0)

我真的不明白。 我尝试使用MySQL,它做了类似于独占连接的东西

mysql> SELECT titre AS tfr, titre AS ten, titre AS tjp FROM data WHERE idlang=1
    -> UNION
    -> SELECT null,titre AS ten, null FROM data WHERE idlang=2
    -> UNION
    -> SELECT null, null, titre as tjp FROM data WHERE idlang=3;
+------------------+------------------+------------------+
| tfr              | ten              | tjp              |
+------------------+------------------+------------------+
| Salut            | Salut            | Salut            |
| Au revoir        | Au revoir        | Au revoir        |
| Je vous en pries | Je vous en pries | Je vous en pries |
| NULL             | Hello            | NULL             |
| NULL             | Goodbye          | NULL             |
| NULL             | You're Welcome   | NULL             |
| NULL             | NULL             | Konnichiha       |
| NULL             | NULL             | Mata ne          |
| NULL             | NULL             | Douitashimashite |
+------------------+------------------+------------------+
9 rows in set (0.00 sec)

如果在第一个SELECT中我做了滴定AS tfr,为null,则列标题变为null。

mysql> SELECT titre AS tfr, titre AS ten, titre AS tjp FROM data WHERE idlang=1
    -> UNION
    -> SELECT null,titre AS ten, null FROM data WHERE idlang=2
    -> UNION
    -> SELECT null, null, titre as tjp FROM data WHERE idlang=3;
+------------------+------------------+------------------+
| tfr              | NULL             | NULL             |
+------------------+------------------+------------------+
| Salut            | NULL             | NULL             |
| Au revoir        | NULL             | NULL             |
| Je vous en pries | NULL             | NULL             |
| NULL             | Hello            | NULL             |
| NULL             | Goodbye          | NULL             |
| NULL             | You're Welcome   | NULL             |
| NULL             | NULL             | Konnichiha       |
| NULL             | NULL             | Mata ne          |
| NULL             | NULL             | Douitashimashite |
+------------------+------------------+------------------+

它看起来仍然不像我想要的结果。

我需要专注于那个数据表以获得所有的一行,但我一直想知道如何实现这一目标。原则上它很简单,但我不能在SQL中翻译它。

mysql> DESCRIBE data;
+-----------+-------------+------+-----+---------+----------------+
| Field     | Type        | Null | Key | Default | Extra          |
+-----------+-------------+------+-----+---------+----------------+
| id        | int(11)     | NO   | PRI | NULL    | auto_increment |
| id_phrase | int(11)     | YES  |     | NULL    |                |
| titre     | varchar(20) | YES  |     | NULL    |                |
| desc      | varchar(50) | YES  |     | NULL    |                |
| idicon    | int(11)     | YES  |     | NULL    |                |
| idlang    | int(11)     | YES  |     | NULL    |                |
+-----------+-------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

实际上idicon对于id_phrase是多余的(不需要它,所以假装它不存在)。

谢谢。

答案 1 :(得分:0)

如果您遇到重新解决的问题,这里有一些提议。

注意:列名和表名可能与原始问题不同,但问题是相同的。

我一直在另一个论坛上提出这个问题,这里有2个我测试的查询,可以证明正在使用MySQL 5.5

查询1:

SELECT id_phrase
    , idicon
    , max(case idlang when 1 then titre end) AS tfr
    , max(case idlang when 1 then DESC  end) AS dfr
    , max(case idlang when 2 then titre end) AS ten
    , max(case idlang when 2 then DESC  end) AS den
    , max(case idlang when 3 then titre end) AS tjp
    , max(case idlang when 3 then DESC  end) AS djp
FROM DATA
    WHERE idlang IN (1, 2, 3)
GROUP BY id_phrase, idicon
ORDER BY id_phrase ASC

查询2:

SELECT t1.id_phrase, t1.idicon, t1.titre AS tfr, t1.descr AS dfr, t2.titre AS ten, t2.descr AS den, t3.titre AS tjp, t3.descr AS djp
FROM DATA AS t1
LEFT OUTER JOIN DATA AS t2
    ON t1.id_phrase=t2.id_phrase
LEFT OUTER JOIN DATA AS t3
    ON t1.id_phrase=t3.id_phrase
WHERE t1.idlang=1 AND t2.idlang=2 AND t3.idlang=3

如果这些查询对您有所帮助,欢迎您。

Source(法语)