嗨,我需要做一个不包含空值的自我加入。
创建表:
CREATE TABLE t2 (
col1 varchar(255),
col2 varchar(255),
col3 varchar(255),
col4 varchar(255),
col5 varchar(255),
col6 varchar(255)
);
--Populate the table:
INSERT INTO t1 (col1, col2, col3, col4, col5, col6)
VALUES
('1', '', 'new1', 'name1','',''),
('2', '', 'new2', 'name2','oldvalue1',''),
('3', '', 'new3', 'name3','',''),
('4', 'value1', 'new4', 'name4','','')
;
--Resulting in:
| col1 | col2 | col3 | col4 | col5 | col6 |
| 1 | | new1 | name1 | | |
| 2 | | new1 | name1 | oldvalue1 | |
| 3 | | new1 | name1 | | |
| 4 | value1 | new1 | name1 | | |
我的更新:
将T2更新为A,T2为B
设置a.col6 = b.col3
其中b.col5 = a.col2;
,但结果是:
| col1 | col2 | col3 | col4 | col5 | col6 |
| 1 | | new1 | name1 | | new1 |
| 2 | | new1 | name1 | oldvalue1 | new1 |
| 3 | | new1 | name1 | | new1 |
| 4 | value1 | new1 | name1 | | |
我想要的是:
| col1 | col2 | col3 | col4 | col5 | col6 |
| 1 | | new1 | name1 | | |
| 2 | | new1 | name1 | oldvalue1 | new1 |
| 3 | | new1 | name1 | | |
| 4 | value1 | new1 | name1 | | |
我在做什么错?
我在Mac上使用mySpql和sequelpro
谢谢!
您是否尝试过?
更新b集b.col6 = a.col3来自T2 a 内部加入t2 b b.col5 = a.col2
感谢您的所有帮助。我弄清楚了我的问题。在桌子中,无效的静止距离只是空白,它们不包含"零"。一旦我更新了表格在空白单元格中具有" null",则一切都起作用。再次感谢您帮助我进行SQL旅程。