Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
加入sql或ms access?_Sql_Ms Access_Join - Fatal编程技术网

加入sql或ms access?

加入sql或ms access?,sql,ms-access,join,Sql,Ms Access,Join,有这样一个SQL或ms access格式的表 样品 ID ID2 name class example 1 10 John main 2 10 3 10 4 10 ~ is look at me. 5 20 Candice main 6 20 7 20 ~ is in Japan. 8 20

有这样一个SQL或ms access格式的表

样品

ID  ID2 name    class   example
1   10  John    main    
2   10          
3   10          
4   10                  ~ is look at me.
5   20  Candice main    
6   20          
7   20                  ~ is in Japan.
8   20                  ~ is reading a book.
9   20          
我需要将示例字段(A)中的“~”替换为name字段的值,该字段的ID2与A和class=“main”相同。如何生成联接语法

结果

ID  ID2 name    class   example
1   10  John    main    
2   10          
3   10          
4   10                  John is look at me.
5   20  Candice main    
6   20          
7   20                  Candice is in Japan.
8   20                  Candice is reading a book.
9   20          

我认为您的桌子配置不正确

事实上,您正在使用一个ID字段,该字段的值在表中重复,这表明数据库设计有点糟糕


我认为在两个表之间分割数据可能会得到更好的结果,一个是示例,另一个是“main”类。然后,您可以使用ID2字段通过简单的联接来联接这两个表。

尽管数据的结构非常糟糕(无论出于何种原因),但要回答您的问题,您可以这样做:

select m.[name] & replace(b.example, "~", "") as combined
from sample as m
inner join sample as b on m.id2 = b.id2
where m.[class] = "main"
and b.example not null 
SELECT 
   T1.[ID],
   T1.[ID2],
   T1.[name],
   T1.[class],
   iif(not isnull(T1.[Example]) and not isnull(T2.[Name]), Replace(T1.[Example], "~", T2.[Name]), null) As [Example]
FROM
   Data T1
LEFT JOIN Data T2 ON (T1.[ID2] = T2.[ID2] AND T2.[Class]="main")
这取决于这样一种假设,即对于ID2的每个唯一值,只有一条class=main的记录(否则将得到重复的行)

如果不需要使用联接,另一种选择是:

SELECT 
   [ID],
   [ID2],
   [name],
   [class],
   (iif(not isnull([example]), Replace([example], "~", nz((select top 1 [name] from Data T2 where T2.ID2 = T1.ID2 and T2.[class]="main" order by T2.[ID2]),"")),null)) as [ExampleNew]
FROM Data T1

... 那就去读一读关于常态的书吧,这并不能回答问题。。。。OP会问:这张表是3NF还是更高?