sql 建表语句:DROP TABLE IF EXISTS `txt_base`;
CREATE TABLE `txt_base` (
  `Id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `f_id` int(11) DEFAULT NULL COMMENT '关联ID',
  `lan_id` int(11) DEFAULT NULL COMMENT '语言ID',
  `txt` varchar(255) DEFAULT NULL COMMENT '文本内容',
  `desc` varchar(255) DEFAULT NULL COMMENT '备注描述',
  PRIMARY KEY (`Id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
INSERT INTO `txt_base` VALUES (1,1,1,'苹果','中文');
INSERT INTO `txt_base` VALUES (2,1,2,'apple','英文');
INSERT INTO `txt_base` VALUES (3,1,3,'pingguo','日文');
INSERT INTO `txt_base` VALUES (4,4,1,'橘子','中文');
INSERT INTO `txt_base` VALUES (5,4,2,'orange','英文');
INSERT INTO `txt_base` VALUES (6,6,1,'香蕉','中文');
/*!40000 ALTER TABLE `txt_base` ENABLE KEYS */;
UNLOCK TABLES;
1、求查询出:
         已翻译成中文,且未被翻译成英文的记录?
2、求查询出:
         已翻译成中文,英文,但是为翻译成日文的记录  (必须中文,英文都已经翻译过,但是日文未被翻译)?sqlselect外连接outer join

解决方案 »

  1.   

    是否翻译通过lan_id区分。
    如果lan_id=2,则代表有英文已翻译,lan_id =1 则代表中文已翻译。
      

  2.   

    试一下
    第一问:select *
      from txt_base a
     where a.f_f_id not in
           (select distinct b.f_f_id from txt_base b where b.f_lan_id = 2);
    第二问:select *
      from txt_base a
     where a.f_f_id not in
           (select distinct b.f_f_id from txt_base b where b.f_lan_id in (3));
    我是用oracle试验的,部分字段你转一下
      

  3.   

    问题是不是没描述清楚,如果描述清楚的话,直接从lan_id查询就行了。说实话搞不懂你的问题
      

  4.   

    首先,语言是否翻译是根据lan_id去区分的,如,lan_id=2则表示已经翻译成英文,没有lan_id=2的记录,就表示没有翻译,如香蕉就没有翻译成英文。1、已翻译成中文,且未被翻译成英文的记录?---》
           即查出香蕉,苹果和橘子都是翻译过的,不能查出来
    2、 已翻译成中文,英文,但是未翻译成日文的记录
           即查出橘子,因为苹果已翻译成日文,不能显示出来,   
                           香蕉的没有翻译成英文,也不能显示出来除了子查询,exists,是否还有外连接的方式?或者其他?
    select a.* from txt_base  a
    where a.f_id  in (select b.f_id from txt_base b where b.f_id=a.f_id and lan_id=1)
      and a.f_id  not in (select b.f_id from txt_base b where b.f_id=a.f_id and lan_id=2 )
     
    select a.* from txt_base  a
    where a.f_id  in (select b.f_id from txt_base b where b.f_id=a.f_id and lan_id=1)
      and a.f_id  in (select b.f_id from txt_base b where b.f_id=a.f_id and lan_id=2)
      and a.f_id not in (select b.f_id from txt_base b where b.f_id=a.f_id and lan_id=3)
      

  5.   

    select * from txt_base t
    where exists(select 1 from txt_base a where a.lan_id=1 and t.f_id = a.f_id) and not exists(select 1 from txt_base b where b.lan_id = 2 and t.f_id = b.f_id);select * from txt_base t
    where exists(select 1 from txt_base a where a.lan_id=1 and t.f_id = a.f_id) 
    and exists(select 1 from txt_base c where c.lan_id=2 and t.f_id = c.f_id) 
    and not exists(select 1 from txt_base b where b.lan_id = 3 and t.f_id = b.f_id);
      

  6.   


    select * from txt_base t
    where exists(select 1 from txt_base a where a.lan_id=1 and t.f_id = a.f_id) and not exists(select 1 from txt_base b where b.lan_id = 2 and t.f_id = b.f_id);select * from txt_base t
    where exists(select 1 from txt_base a where a.lan_id=1 and t.f_id = a.f_id) 
    and exists(select 1 from txt_base c where c.lan_id=2 and t.f_id = c.f_id) 
    and not exists(select 1 from txt_base b where b.lan_id = 3 and t.f_id = b.f_id); 
      

  7.   

     select * from tab;