首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

求相助写一个外连接SQL!

2013-02-15 
求帮助写一个外连接SQL!!!!!!!!本帖最后由 szqfsx123 于 2013-01-30 16:24:06 编辑sql 建表语句:DROP TABL

求帮助写一个外连接SQL!!!!!!!!
本帖最后由 szqfsx123 于 2013-01-30 16:24:06 编辑 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、求查询出:
         已翻译成中文,英文,但是为翻译成日文的记录  (必须中文,英文都已经翻译过,但是日文未被翻译)? sql select 外连接 outer?join
[解决办法]
1
select * from txt_base a
 where exists(select 1 from txt_base where f_id=a.f_id and txt='中文')
       and not exists(select 1 from txt_base where f_id=a.f_id and txt='英文')
2
select * from txt_base a
 where exists(select 1 from txt_base where f_id=a.f_id and txt='中文')
       and   exists(select 1 from txt_base where f_id=a.f_id and txt='英文')
       and not exists(select 1 from txt_base where f_id=a.f_id and txt='日文')
[解决办法]



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 txt='中文')
  and a.f_id not in (select b.f_id from txt_base b where b.f_id=a.f_id and txt='中文')


[解决办法]





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 txt='中文')
  and a.f_id not in (select b.f_id from txt_base b where b.f_id=a.f_id and txt='英文')


[解决办法]


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 txt='中文')
  and a.f_id  in (select b.f_id from txt_base b where b.f_id=a.f_id and txt='英文')
  and a.f_id not in (select b.f_id from txt_base b where b.f_id=a.f_id and txt='日文')


[解决办法]
 经过纠正 的




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)

[解决办法]
select * from 
(select * from txt_base where f_id=1) a
left join
(select * from txt_base where f_id=2) b
on a.f_id=b.f_id
where b.f_id is null

热点排行