SQL - 在查询中连接同一个表中的列
解决了!请看下面的答案!
在解释我的问题之前,我想为那些觉得这个问题太长的人道歉,但我觉得我必须提供一些细节以使事情变得更清楚。虽然这个问题很容易理解,但对我来说实施起来却并不那么容易。
我有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
orIcon1|FlagIcon1|FlagIcon2|FlagIcon3|TitreLang1|DescLang1|TitreLang2|DescLang2|TitreLang3|DescLang3
or evenIcon1|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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我真的不明白。
我尝试过使用 MySQL,它会执行类似独占连接的操作,
如果在第一个 SELECT 中我执行 titre AS tfr, null, null ,则列标题将变为 null。
看起来还是不是我想要的结果。
我需要专注于该数据表以将所有内容整合到一行中,但我一直想知道如何实现这一目标。原则上它很简单,但我无法用 SQL 翻译它。
实际上 idicon 与 id_phrase 是多余的(实际上并不需要它,所以假装它不存在)。
谢谢。
I really don't get it.
I've tried with MySQL and it does something like an exclusive join
If in the 1st SELECT I do titre AS tfr, null, null the column headers get to null.
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.
Actually idicon is redundant with id_phrase (don't really need it so pretend it does not exist).
Thank you.
如果您遇到类似的问题,这里有一些建议。
注意:列名和表名可能与原始问题不同,但问题是相同的。
我一直在另一个论坛上问这个问题,这里有 2 个我测试过的查询,可以证明它们正在使用 MySQL 5.5
查询 1:
查询 2:
如果这些查询对您有帮助,不客气。
来源(法语)
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 :
Query 2 :
You're welcome if these queries may help you.
Source (french)