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

发布于 2024-12-04 10:16:36 字数 5492 浏览 1 评论 0原文

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

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

我有3张桌子。

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

[Hata]

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 表中没有任何具体内容,一切都会好起来的。

事实上,对于每个图标都有 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|DESLang

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

甚至 Joined

换句话说,这就像我将多个查询连接在一起,例如

SELECT icon FROM Icon

with

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'

Joined With

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 参考以尝试很多事情,但它们似乎没有达到我的预期(连接、联合等...) 我还尝试了以下查询,但它给了我一条错误消息。

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 没有实现,但是代码被着色了,所以我认为应该没问题。

感谢您的配合和帮助。

SOLVED ! See the answer bellow !

Before I explain my problem I want to apologise for those who would feel this question is too long but I feel like I must give some details to make things the clearer possible. Though, the problem is simple to understand it is not that simple to me to implement.

I have 3 tables.

Hata and Icon contains images I want to link with Succes which contains texts

[Hata]

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)

Here is a sample showing how the Succes table looks like

+----+----------+-----------------+------------------+--------+
| 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  |
+----+----------+-----------------+------------------+--------+
...

The tables are now joined using this WHERE conditions

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

Everything would be fine if there would be nothing specific in the Succes table.

In fact, for each Icon there are 3 sentences and the idPhrase links them but in the actual result set I somehow have redundancies.

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

What I'd like to achieve is the following (just the very first row):

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

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

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

In other words, it would be like I'd joined several queries together such as

SELECT icon FROM Icon

Joined with

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'

Joined With

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'

And so on...
Just the problem of ensuring the links between tables (icon 1 with sentence 1)

Here's another sample on how it should (may) look like

+-------+-------+-------+-------+----------------+------------------+------------+-----------------+----------------+------------------+
| 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 |
+-------+-------+-------+-------+----------------+------------------+------------+-----------------+----------------+------------------+
...

I've browsed for SQL reference to try many things but they don't seem to do what I expect (CONCATENATE, UNION, etc...)
I also tried the following query but it gives me an error message.

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'

the message is

Statut SQL: HY000
Error Code: 1000

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

but it seems my syntax is good according to sample I've found even on StackOverflow.
I must also specify that I'm using OpenOffice Base and my purpose is publishing a document. Maybe there is something specific to OOo such as LEFT JOIN not implemented but the code get coloured so I think it should be fine.

Thank you for your availability and help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独自←快乐 2024-12-11 10:16:36

我真的不明白。
我尝试过使用 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 中我执行 titre AS tfr, null, 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 是多余的(实际上并不需要它,所以假装它不存在)。

谢谢。

I really don't get it.
I've tried with MySQL and it does something like an exclusive join

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)

If in the 1st SELECT I do titre AS tfr, null, null the column headers get to 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 |
+------------------+------------------+------------------+

It still doesn't looks like the result I want.

I need to concentrate on that data table to get all in one line but I keep wondering how to achieve this. In principle it is very simple but I can't translate that in 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)

Actually idicon is redundant with id_phrase (don't really need it so pretend it does not exist).

Thank you.

凉城凉梦凉人心 2024-12-11 10:16:36

如果您遇到类似的问题,这里有一些建议。

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

我一直在另一个论坛上问这个问题,这里有 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

如果这些查询对您有帮助,不客气。

来源(法语)

HERE ARE SOME PROPOSITIONS IF YOU WOULD MEET A RESEMBLING PROBLEM.

NB: The column names and table names may differ from the original question but the problem is the same.

I've been asking this question on another forum and here are 2 queries I tested and can certify are working with MySQL 5.5

Query 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

Query 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

You're welcome if these queries may help you.

Source (french)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文