我有一个翻译数据库,我需要找到未翻译成某些核心语言的变量,以及被翻译成一种特定语言而不是另一种语言的变量。
数据库的简化版本如下:
CREATE TABLE var (
id int unsigned primary key auto_increment,
name varchar(15)
);
CREATE TABLE language (
id smallint unsigned primary key auto_increment,
name varchar(31),
ietf varchar(5)
);
CREATE TABLE translation (
var_id int unsigned not null,
lang_id smallint unsigned not null,
val varchar(128),
PRIMARY KEY(var_id, lang_id),
CONSTRAINT trans_fk_var
FOREIGN KEY(var_id)
REFERENCES var(id),
CONSTRAINT trans_fk_lang
FOREIGN KEY(lang_id)
REFERENCES language(id)
);
insert into var (name) values ('var1'),('var2'),('var3');
insert into language (name, ietf) values ('German', 'de'), ('Spanish', 'es'), ('German (Austria)', 'de-AT'), ('Spanish (Mexico)', 'es-MX');
insert into translation values (1,1, 'string 1'), (1,2, 'string 2'), (1,3, 'string 3'), (2,1, 'string 4'), (2,2, 'string 5'), (3,1, 'string 6');
我要找出的查询是:
所述查询将相当频繁地执行,并且数据库很大,所以我需要查询尽可能便宜。
答案 0 :(得分:0)
您可以使用group by
和having
。因此,您的第一个问题可以表述为:
select var_id
from translations t
group by var_id
having count(*) < (select count(*) from language);
第二个为:
select var_id
from translations t
group by var_id
having sum(lang_id = 2) > 0 and
sum(lang_id = 4) = 0;