如果是单表查询出现几条一样:说明数据表本身存在多条数据,需要查看代码是否有问题。
如果是多表关联查询出现几条,单表查询出现一条:说明是查询SQL问题,关联表中的关联字段数据有多条,导致查询出来有多条一样。
select user_code,user_name,related_code from tablename t left join tablename s on t.user_code=s.related_code
根据你提供的测试数据,输出结果还是这样,不变。
------------------------------------------------------
通过你补充了问题说明,我分析了下,程序如下:
SQL> select * from re_a
USER_CODE USER_NAME RELATED_CODE
---------- -------------------------------- ------------
1001 张三 1002
1002 李四 1001
1003 王五 1004
1004 赵六 1003
SQL> select * from re_bak
USER_CODE USER_NAME RELATED_CODE
---------- -------------------------------- ------------
SQL> exec pro_relation_baidu
1001张三1002
1003王五1004
PL/SQL procedure successfully completed
SQL> create or replace procedure pro_relation_baidu IS
/*CREATE TABLE re_a(user_code NUMBER,user_name VARCHAR(32),related_code NUMBER)
INSERT INTO RE_A VALUES ('1001', '张三', '1002')
INSERT INTO RE_A VALUES ('1002', '李四', '1001')
INSERT INTO RE_A VALUES ('1003', '王五', '1004')
INSERT INTO RE_A VALUES ('1004', '赵六', '1003')
SELECT * FROM re_a
create table re_bak as select * from re_a where 1=2*/
CURSOR cur_rea IS SELECT * FROM re_a
CURSOR cur_bak IS SELECT * FROM re_bak
--v_rea re_a%ROWTYPE
j NUMBER
BEGIN
DELETE re_bak
FOR i IN cur_rea LOOP
SELECT COUNT(t.USER_CODE) INTO j FROM re_bak t WHERE
t.USER_CODE=i.RELATED_CODE AND t.RELATED_CODE=i.USER_CODE
IF j=0 THEN
INSERT INTO re_bak VALUES(i.USER_CODE,i.USER_NAME,i.RELATED_CODE)
ELSE
NULL
END IF
END LOOP
FOR i IN cur_bak LOOP
dbms_output.put_line(i.USER_CODE||i.USER_NAME||i.RELATED_CODE)
END LOOP
end pro_relation_baidu
select tu.*, bu.user_name as ld_namefrom TB_USER as tu
left join TB_USER as bu on tu.zjld_id = bu.user_id
and tu.user_id = '1'
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)