基本上,有一个属性表和转换表——一个属性有多个转换.

我需要为指定语言中的每个属性从翻译中 Select id和值,即使该语言中没有翻译记录.要么我缺少一些连接技术,要么join(不涉及语言表)在这里不起作用,因为以下内容不会返回指定语言中不存在翻译的属性.

select a.attribute, at.id, at.translation 
from attribute a left join attributeTranslation at on a.id=at.attribute
where al.language=1;

所以我使用这样的子查询,这里的问题是使用相同的参数对同一个表进行两个子查询(感觉像是性能消耗,除非MySQL对这些子查询进行分组,我对此表示怀疑,因为它会让您执行许多类似的子查询)

select attribute, 
(select id from attributeTranslation where attribute=a.id and language=1),
(select translation from attributeTranslation where attribute=a.id and language=1), 
from attribute a;

我希望能够从一个查询中获取id和翻译,所以我在后面分析列并从字符串中获取id,这至少是一个子查询,但看起来仍然不正确.

select attribute,
(select concat(id,';',title)
    from offerAttribute_language 
    where offerAttribute=a.id and _language=1
)
from offerAttribute a

所以问题部分.

[[attribute to language]to translation](连接3个表似乎比子查询的性能更差).

推荐答案

是的,你能做到.您需要的诀窍是,有两种方法可以从表服务器中取出表.一种方法是..

FROM TABLE A

另一种方式是

FROM (SELECT col as name1, col2 as name2 FROM ...) B

请注意,select子句及其周围的括号是一个表,一个虚拟表.

因此,使用您的第二个代码示例(我猜您希望在这里检索的列):

SELECT a.attr, b.id, b.trans, b.lang
FROM attribute a
JOIN (
 SELECT at.id AS id, at.translation AS trans, at.language AS lang, a.attribute
 FROM attributeTranslation at
) b ON (a.id = b.attribute AND b.lang = 1)

请注意,您的实际表attribute是这个联接中的第一个表,我调用的这个虚拟表b是第二个表.

当虚拟表是某种类型的汇总表时,这种技术尤其有用.例如

SELECT a.attr, b.id, b.trans, b.lang, c.langcount
FROM attribute a
JOIN (
 SELECT at.id AS id, at.translation AS trans, at.language AS lang, at.attribute
 FROM attributeTranslation at
) b ON (a.id = b.attribute AND b.lang = 1)
JOIN (
 SELECT count(*) AS langcount,  at.attribute
 FROM attributeTranslation at
 GROUP BY at.attribute
) c ON (a.id = c.attribute)

看到了吗?您已经生成了一个包含两列的虚拟表c,将其与另外两列联接,将其中一列用于ON子句,并将另一列作为结果集中的一列返回.

Mysql相关问答推荐

无法让我的SQL Select陈述与USE一起使用

嵌套MySQL语句问题

为分组记录MySQL取取值为TRUE的单行

MySQL MDL(元数据锁)为什么会导致死锁?

生成json数据并根据特定表的匹配条件进行过滤

带有 JOIN 和 WHERE 子句的 INSERT 语句

global max_connections 和 spring.hikari.maximumPoolSize 有什么区别?

Mysql根据文本语言或行数将列拆分为多列

MySql SELECT 查找包含数字的区间的时间复杂度是多少?

如何获取从开始时间到结束时间的所有正在运行的作业(job)的总和?

如何在任何连续范围内获得最大值

计算每个用户在第二个表中有多少条记录

使用 MySQL LIMIT、OFFSET 进行分页

自动登录 phpMyAdmin

mysql 按日期 Select 总和组

INSERT ... 使用 WHERE 进行重复的密钥更新?

将sql查询的结果写入mysql中的文件

MySQL与空值比较

比较 MySQL 中的日期忽略 DateTime 字段的时间部分

"SELECT COUNT(*)" 很慢,即使有 where 子句