A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© xiewen 中级黑马   /  2013-6-8 16:05  /  2189 人查看  /  13 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 xiewen 于 2013-7-27 17:31 编辑

--联系人表
create table user_touch
(
        id int primary key identity(1,1),
        name nvarchar(30) not null,
        touch_num int references touch(id)
)
--联系方式表
create table touch
(
        id int primary key identity(1,1),
        phone varchar(11),
        home_tel varchar(15),
        company_tel varchar(15)
)
--向联系方式表插入数据
insert into touch values('15697553163',null,null)
insert into touch values('15814458712','0796-5552453',null )
insert into touch values('15678536685','0796-5853387',null)
insert into touch values('13845569569','010-16899736','0755-64569522')
insert into touch values('13717834951',null,null)

--向联系人表插入数据
insert into user_touch
select '谢文',2 union
select '曾亮',1 union
select '郑雄',3 union
select '吴楚耿',4union
select '肖楚',5
--------------查询只有一种联系方式的员工

评分

参与人数 1技术分 +1 收起 理由
袁梦希 + 1 很给力!

查看全部评分

13 个回复

倒序浏览
本帖最后由 xiewen 于 2013-6-8 16:41 编辑

谁给我解答一下啊
回复 使用道具 举报
不太明白这个是要问什么,感觉答案都已经给出了
大概是我SQL丢了太久,不过还是可以看出是创建两个表并且插入测试数据,这个用普通的增加语句就可以解决
然后最后是查询一种联系方式的员工  这个我想查询联系方式 字段为Phone或者为company_tel有且仅有一个就可以了,也就是查询表中这两个字段有且只有一个不为NULL,然后用得到的ID查询员工ID,具体联查的语句抱歉丢了太久不能给出。
希望能给你带来帮助。

评分

参与人数 1技术分 +1 收起 理由
袁梦希 + 1 很给力!

查看全部评分

回复 使用道具 举报
select name
from user_touch,touch
where touch values=1;

评分

参与人数 1技术分 +1 收起 理由
袁梦希 + 1 很给力!

查看全部评分

回复 使用道具 举报
黄辉 发表于 2013-6-8 17:22
不太明白这个是要问什么,感觉答案都已经给出了
大概是我SQL丢了太久,不过还是可以看出是创建两个表并且插 ...

有三种联系方式
回复 使用道具 举报
夏一站 发表于 2013-6-8 17:29
select name
from user_touch,touch
where touch values=1;

你这个sql语句错了
回复 使用道具 举报
xiewen 发表于 2013-6-8 18:46
有三种联系方式

抱歉看掉了一个,我觉得思路大概就是这样,楼主应该比我扎实,我是1年多前做实施用过一会SQL语句,忘掉很久了····
回复 使用道具 举报
加油   问题很有思想
回复 使用道具 举报
楼主你好  如果问题已经解决  请把帖子的类型改为已解决  然后会给你发几个粽子吃
回复 使用道具 举报
忘了怎么写比较简单了,但是这样写应该可以的
select name from touch where
  (phone!=null and home_tel=null and company_tel=null) or
  (phone=null and home_tel=!null and company_tel=null) or
  (phone=null and home_tel=null and company_tel=!null);
回复 使用道具 举报
  1. select name  from user_touch as u ,touch as t where
  2.                                          u.touch =t.identity   
  3.                                        and  
  4.     ((phone!=null and home_tel=null and company_tel=null) or
  5.   (phone=null and home_tel=!null and company_tel=null) or
  6.   (phone=null and home_tel=null and company_tel=!null))
复制代码
回复 使用道具 举报
select * from user_touch a left join touch b on a.touch_num=b.id where
(b.phone is null and  b.home_tel is null) or (b.company_tel is null and b.home_tel is null)
or ( b.phone is null and b.company_tel is null)
回复 使用道具 举报
好难啊 好难啊
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马