简体   繁体   English

SQL-查询中来自同一表的列

[英]SQL - Joining columns from the same table in a Query

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. 我有3张桌子。

Hata and Icon contains images I want to link with Succes which contains texts 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)

Here is a sample showing how the Succes table looks like 这是显示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  |
+----+----------+-----------------+------------------+--------+
...

The tables are now joined using this WHERE conditions 现在使用此WHERE条件联接表

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

Everything would be fine if there would be nothing specific in the Succes table. 如果Succes表中没有任何特定内容,一切都会很好。

In fact, for each Icon there are 3 sentences and the idPhrase links them but in the actual result set I somehow have redundancies. 实际上,对于每个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
...

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 Icon1|FlagIcon1|FlagIcon2|FlagIcon3|TitreLang1|DescLang1|TitreLang2|DescLang2|TitreLang3|DescLang3

or even Icon1|FlagIcon1|FlagIcon2|FlagIcon3|TitreLang1|TitreLang2|TitreLang3|DescLang1|DescLang2|DescLang3 甚至是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) 依此类推...确保表之间链接的问题(带有句子1的图标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. 我浏览了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'

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. 但是根据我在StackOverflow上发现的示例,看来我的语法很好。 I must also specify that I'm using OpenOffice Base and my purpose is publishing a document. 我还必须指定我正在使用OpenOffice Base,目的是发布文档。 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. 也许有一些特定于OOo的东西,例如LEFT JOIN没有实现,但是代码变色了,所以我认为应该没问题。

Thank you for your availability and help. 多谢您的协助与协助。

I really don't get it. 我真的不明白。 I've tried with MySQL and it does something like an exclusive join 我已经尝试过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)

If in the 1st SELECT I do titre AS tfr, null, null the column headers get to null. 如果在第一个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 |
+------------------+------------------+------------------+

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. 原则上,这非常简单,但我无法在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). 实际上,idicon与id_phrase是多余的(并不需要它,因此假装它不存在)。

Thank you. 谢谢。

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 我一直在另一个论坛上问这个问题,这是我测试并可以证明正在使用MySQL 5.5的2个查询

Query 1 : 查询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 : 查询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) 来源 (法语)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM