2017-01-16 56 views
0

为什么没有获得跨产品类型的记录?执行两张表时没有记录

如果表test2有记录testName。 和表test1没有任何记录。

select *from test2 
column name : name 
column value : testName 

select *from test1 
column name : id 
column value : 

select *From test1,test2 
id  name 

*no any records 

我想要这样的记录。

id  name 
null testName 
+0

在此处添加dbms标记 –

+0

您正在此处进行交叉连接。你的目标是什么? – GurV

回答

0

您可以使用LEFT JOIN如果你想获取可能会或可能不会在其他表中存在的记录:

select * 
From test2 b 
left join test1 a ON a.value = b.value 
+0

感谢您的回复!让我检查 – Vinod

+0

test2中没有'name'列 – GurV

+0

更新请检查。 – Susang

0

尝试以下:

SELECT t1.id, t2.name 
FROM test2 AS t2 LEFT JOIN 
    test1 AS t1 ON 1=1 
WHERE t2.name LIKE 'testName' 
    AND NOT EXISTS (SELECT 1 FROM test1) 

让我知道如果这不起作用。

+0

表test2没有列'id' – Vinod

+0

好的,给一秒,我会改变答案。 – PawelCz

+0

感谢您的快速响应 – Vinod

1

使用全加入与将始终评估为true会得到你想要的结果的条件:

创建和填充示例表(保存我们这一步在你未来的问题)

create table test1 
(
    id int 
) 

create table test2 
(
    name varchar(10) 
) 

insert into test2 values ('testName') 

查询:

select * 
from test1 
full join test2 on 1=1 

结果:

id name 
NULL testName 
+0

感谢您的工作! – Vinod

+0

[很高兴帮助: - )](http://meta.stackoverflow.com/questions/291325/how-to-show-appreciation-to-a-user-on-stackoverflow/291327#291327) –

相关问题