MySQL:如何从集合中删除行?

发布于 2024-12-28 11:14:47 字数 995 浏览 3 评论 0原文

我有一个翻译请求: (表结构:

  1. line_id
  2. lang(“en”或“ru”)
  3. 文本

,因此,在表中它看起来像:

5 | en | Test
6 | en | Hello!
7 | en | Another words
6 | ru | Привет!

并且所有翻译都在带有键line_id的同一个表中,与语言无关)

SELECT txt.line_id, txt.text AS o, ru.text AS t 
  FROM text AS txt, text AS ru 
 WHERE txt.line_id = ru.line_id 
   AND txt.lang = 'en';

它将返回这样的数组

> [5] => Array ( [line_id] => 5 [o] => Test [t] => Test 
> [6] => Array ( [line_id] => 6 [o] => Hello! [t] => Hello! ) 
> [7] => Array ( [line_id] => 6 [o] => Hello! [t] => Привет! )

o - 是原文,t - 翻译。

如何从集合 #6 行中删除,因为我们在下一行中有翻译。 GROUP BY 将删除#7 行并保存#6 行。

最好的结果是:

> [5] => Array ( [line_id] => 5 [o] => Test [t] => ) 
> [6] => Array ( [line_id] => 6 [o] => Hello! [t] => Привет! )

没有 [o] =>你好! [t] =>你好!

I have a request of translation:
(table structure:

  1. line_id
  2. lang ("en" or "ru")
  3. text

so, in the table it looks like:

5 | en | Test
6 | en | Hello!
7 | en | Another words
6 | ru | Привет!

and all translations are in the same table with key line_id, that is independent from the language)

SELECT txt.line_id, txt.text AS o, ru.text AS t 
  FROM text AS txt, text AS ru 
 WHERE txt.line_id = ru.line_id 
   AND txt.lang = 'en';

it will return such array

> [5] => Array ( [line_id] => 5 [o] => Test [t] => Test 
> [6] => Array ( [line_id] => 6 [o] => Hello! [t] => Hello! ) 
> [7] => Array ( [line_id] => 6 [o] => Hello! [t] => Привет! )

o - is original text, t - translation.

How to delete from set #6 row, because we have translation in the next row. GROUP BY will kill #7 and save #6 row.

the best result would be:

> [5] => Array ( [line_id] => 5 [o] => Test [t] => ) 
> [6] => Array ( [line_id] => 6 [o] => Hello! [t] => Привет! )

without [o] => Hello! [t] => Hello!

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

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

发布评论

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

评论(4

森罗 2025-01-04 11:14:47

我只是猜测你可能需要:

SELECT txt.line_id, 
         txt.text AS o, 
         COALESCE (ru.text,'UNTRANSLATED') AS t 
FROM 
  text AS txt
LEFT JOIN 
  text AS ru 
ON txt.line_id=ru.line_id AND ru.lang='ru'
WHERE txt.lang='en'

I'm just guessing you probably need:

SELECT txt.line_id, 
         txt.text AS o, 
         COALESCE (ru.text,'UNTRANSLATED') AS t 
FROM 
  text AS txt
LEFT JOIN 
  text AS ru 
ON txt.line_id=ru.line_id AND ru.lang='ru'
WHERE txt.lang='en'
第几種人 2025-01-04 11:14:47

按 lang desc 排序(所以 ru 位于顶部)然后按 line_id 分组怎么样?

How about sort by lang desc (so ru is on top) and then group by line_id?

陌若浮生 2025-01-04 11:14:47

从翻译表中排除原始语言

SELECT txt.line_id, txt.text AS o, ru.text AS t 
FROM text AS txt, text AS ru 
WHERE txt.line_id=ru.line_id 
   AND txt.lang='en' 
   AND ru.lang!='en'

新版本的新描述

SELECT txt.line_id, ru.lang, ru.text AS t 
FROM text AS txt, text AS ru 
WHERE txt.line_id=ru.line_id 
   AND txt.lang='en'
ORDER BY text.lang, txt.line_id

exclude the original language from the translation table

SELECT txt.line_id, txt.text AS o, ru.text AS t 
FROM text AS txt, text AS ru 
WHERE txt.line_id=ru.line_id 
   AND txt.lang='en' 
   AND ru.lang!='en'

new version for new description

SELECT txt.line_id, ru.lang, ru.text AS t 
FROM text AS txt, text AS ru 
WHERE txt.line_id=ru.line_id 
   AND txt.lang='en'
ORDER BY text.lang, txt.line_id
孤者何惧 2025-01-04 11:14:47

为了列出已翻译的行和未经翻译的原始行,您必须面对 MySql 中缺乏对访问同一表的子查询的支持。

在我看来,你可以划分问题:首先用一个 SELECT 列出没有翻译的原始行;然后用另一个 SELECT 列出翻译;最后使用UNION。

-- original without translations
SELECT txt.line_id, txt.text AS o, ru.text AS t 
FROM text AS txt LEFT JOIN text AS ru 
ON(txt.line_id = ru.line_id AND txt.lang = 'en' AND ru.lang != 'en')
WHERE ru.line_id IS NULL -- ensures no translations have been found

UNION

-- only matching translations
SELECT txt.line_id, txt.text AS o, ru.text AS t
FROM text AS txt JOIN text AS ru 
ON(txt.line_id = ru.line_id AND txt.lang = 'en' AND ru.lang != 'en');

此致。

In order to list translated rows and original rows without translation, you must face the lack of support in MySql for sub queries that access the same table.

In my opinion you could divide the problem: first list original rows without translations with one SELECT; then list translations with another SELECT; in the end use a UNION.

-- original without translations
SELECT txt.line_id, txt.text AS o, ru.text AS t 
FROM text AS txt LEFT JOIN text AS ru 
ON(txt.line_id = ru.line_id AND txt.lang = 'en' AND ru.lang != 'en')
WHERE ru.line_id IS NULL -- ensures no translations have been found

UNION

-- only matching translations
SELECT txt.line_id, txt.text AS o, ru.text AS t
FROM text AS txt JOIN text AS ru 
ON(txt.line_id = ru.line_id AND txt.lang = 'en' AND ru.lang != 'en');

Best regards.

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