简单的公主头发型扎法:SQL中联接数据时如何筛选

来源:百度文库 编辑:高校问答 时间:2024/04/28 13:47:16
进行数据库联接时,我想在 info_comment表列不同记录值,但是提示“当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。”请问如何才能在info_comment中筛选?
From information INNER JOIN info_comment ON information.info_id = (select DISTINCT info_id from info_comment)
谢谢"沉默用户"、"yangmeicrazy"二位的回答,我发现
ON information.info_id =info_comment.Info_id
这种联接是一对多,比如这样:
表:information:
info_id(1)
title (计算机)

表:info_comment
info_id(1)
title (图形处理)
----------------
info_id(1)
title (编程)
-----------------
info_id(1)
title (数据库)

如何能实现一对一的关系呢?只要info_comment表选出一条符合条件的就可以了

楼主可以这样写

From information INNER JOIN (select DISTINCT info_id from info_comment) info_comment
ON information.info_id =info_comment.Info_id

information.info_id = (select DISTINCT info_id from info_comment)这种
写法本身就有问题。子查询select DISTINCT info_id from info_comment的结果只有一条记录或没有记录时,是可以这么写的,但这样写可不是个好习惯。

select (DISTINCT) 字段名1,字段名2…… from information as a inner join info_comment as b on a.info_id=b.info_id
不去掉B表中重复的,结果会出现的重复的吗,你用的是内连接呀.如果还有重的,就在字段名前加DISTINCT,我觉得效果是一样的.
你试试,如果还不行的话,你写详细点,我再帮你看看.