简体   繁体   English

使用CROSS JOIN

[英]Using CROSS JOIN

I am learning how to use CROSS JOINS and I can't see what I am doing wrong with the one I am currently attempting. 我正在学习如何使用CROSS JOINS,但看不到我正在尝试的错。

What I have is a Pattern and Numbers table. 我有一个PatternNumbers表。

Number Table: 编号表:
Numberid TinyINT 编号TinyINT

Pattern Table: 模式表:
PatternID TinyINT IDentity(1,1) PatternID TinyINT IDentity(1,1)
Patternresult varchar(5) Patternresult varchar(5)

I have already inserted each row in the Number table from 1 - 22. 我已经从1-22插入了Number表中的每一行。

At the moment I am getting no results displayed. 目前,我没有显示任何结果。 But what I want to display is all the patterns between only numbers 0 - 5 (I can't include '0' in the number table as later on I will be using a table that requires a number beginning at '1' from the 'Number' table 但是我想显示的是仅0到5之间的所有模式(我不能在数字表中包含“ 0”,因为稍后我将使用一个表,该表要求从“编号表

e.g

0 - 0
0 - 1
0 - 2
0 - 3
0 - 4
0 - 5
1 - 0
1 - 1
1 - 2
1 - 3
1 - 4
1 - 5

etc

What am I doing incorrectly with my CROSS JOIN? 我的CROSS JOIN做错了什么?

INSERT INTO dbo.Pattern(PatternResult)
SELECT cast(n.NumberID AS VARCHAR (5)) + ' - ' + cast(nn.NumberID AS VARCHAR (5)) AS PatternResult
FROM dbo.Number n
CROSS JOIN dbo.Number nn

Here you go.i have added ">0" in my query ,since i have numbers table starting from 0 ,you can ignore that.. 我在查询中添加了“> 0”,因为我的数字表从0开始,所以您可以忽略它。

    ;With Cte
    as
    (
    select 0 as n
    union all
    select top 5 Number from numbers where number>0
    order by number
    )
Insert into dbo.pattern
(
PatternResult
)
    select cast(c.n as varchar)+'-'+b.n
     from cte c
    cross join
    (select cast(n as varchar) from cte) b(n)
    order by c.n

Output: 输出:

0-0
0-1
0-2
0-3
0-4
0-5
1-0 and so on

your problems your number contains number from 1 to 22 and the PatternResult is only varchar(5) 您的问题,您的数字包含从1到22的数字,并且PatternResult仅是varchar(5)

so when you concatenate a 2 digits number, you will get PatternResult like '12 - 13' , which the length exceeded 5 因此,当您连接2位数字时,您将获得PatternResult之类的'12-13',其长度超过5

And so your query should get truncation error and hence no result in the Pattern table. 因此,您的查询应该出现截断错误,因此在Pattern表中没有结果。

Change the PatternResult to varchar(10) and it should work. 将PatternResult更改为varchar(10),它应该可以工作。

First, your query is an insert . 首先,您的查询是insert Insert statements do not return result sets. Insert语句不返回结果集。

So, the question is, what does this return? 因此,问题是,这将返回什么?

select *
from dbo.Pattern

Next, the pattern has space for five characters, but you are already using up three with ' - ' . 接下来,模式有五个字符的空间,但是您已经用完了' - '三个字符。 So, try making it so the pattern can fit (by removing the spaces): 因此,请尝试使其适合模式(通过删除空格):

INSERT INTO dbo.Pattern(PatternResult)
    SELECT cast(n.NumberID AS VARCHAR (5)) + '-' + cast(nn.NumberID AS VARCHAR (5)) AS PatternResult
    FROM dbo.Number n CROSS JOIN
         dbo.Number nn;

Finally, when using VARCHAR() there really is no advantage to making the sizes too small. 最后,使用VARCHAR()时,使尺寸过小确实没有任何好处。 You might as well define the field as something like Patternresult varchar(255) so it will always be big enough for any result. 您最好将字段定义为Patternresult varchar(255)类的东西,这样它对于任何结果都将始终足够大。 When using CHAR() then a small value is important, because all the extra characters are stored as spaces. 使用CHAR()一个很小的值很重要,因为所有其他字符都存储为空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM