假设我有一个MySQL表product
,如下所示:
+-------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| id | varchar(36) | NO | PRI | NULL | |
| lang | varchar(255) | YES | | NULL | |
| description | varchar(255) | YES | | NULL | |
+-------------+--------------+------+-----+---------+-------+
我想把这product
条信息翻译成几种语言.目前只有description
字段是可翻译的,但我们将在future 添加更多可翻译的字段.
为了启用转换,我创建了一个新的表product_translation
,如下所示:
CREATE TABLE product_translation (
id VARCHAR(36) PRIMARY KEY,
language_code VARCHAR(10),
product_id VARCHAR(36),
description VARCHAR(255),
FOREIGN KEY (product_id) REFERENCES product(id)
);
现在,如果请求的语言是英语或不是英语,我将使用product
表的description
字段.否则,我会像这样加入product
和product_translation
表:
SELECT product.*,
product_translation.language_code,
product_translation.description
FROM product INNER JOIN product_translation
ON product_translation.product_id = product.id
WHERE product_translation.language_code = 'fr';
Does it make sense ?
Can I use one SQL statement to retrieve products for any language ?
Would it be better to drop the description
field from the product
table, copy the data, and always use the join ?