gpt4 book ai didi

SQL随机名称生成器不在同一行中插入名字和姓氏

转载 作者:行者123 更新时间:2023-12-02 07:25:55 27 4
gpt4 key购买 nike

我设法创建了一个简单的查询,它随机选择名字和姓氏并将它们插入到结果表中。我想创建一些可以与我运行的各种测试互换的东西,我必须在这些测试中制造大量数据。这是代码(为了简单起见,我只包含了 5 个名字和姓氏):

SELECT 
FirstName, LastName
FROM
(SELECT TOP 1
FirstName
FROM
(SELECT 'John' AS FirstName
UNION SELECT 'Tim' AS FirstName
UNION SELECT 'Laura' AS FirstName
UNION SELECT 'Jeff' AS FirstName
UNION SELECT 'Sara' AS FirstName) AS First_Names
ORDER BY NEWID()) n1
FULL OUTER JOIN
(SELECT TOP 1
LastName
FROM (SELECT 'Johnson' AS LastName
UNION SELECT 'Hudson' AS LastName
UNION SELECT 'Jackson' AS LastName
UNION SELECT 'Ranallo' AS LastName
UNION SELECT 'Curry' AS LastName) AS Last_Names
ORDER BY NEWID()) n2 ON [n1].FirstName = [n2].LastName
WHERE
n1.FirstName IS NOT NULL OR n2.LastName IS NOT NULL

结果如下:

FirstName LastName 
NULL Hudson
John NULL

我希望结果返回一行,其中随机生成名字和姓氏,以便每一行都有一个完整的名称(没有 NULL 值)。我确定我忽略了一些简单的事情。

最佳答案

问题是您的加入。这是你可以做到的:

SELECT FirstName, LastName
FROM
(SELECT TOP 1 FirstName
FROM (SELECT 'John' AS FirstName
UNION SELECT 'Tim' AS FirstName
UNION SELECT 'Laura' AS FirstName
UNION SELECT 'Jeff' AS FirstName
UNION SELECT 'Sara' AS FirstName) AS First_Names ORDER BY NEWID())n1
CROSS JOIN
(SELECT TOP 1 LastName
FROM (SELECT 'Johnson' AS LastName
UNION SELECT 'Hudson' AS LastName
UNION SELECT 'Jackson' AS LastName
UNION SELECT 'Ranallo' AS LastName
UNION SELECT 'Curry' AS LastName) AS Last_Names ORDER BY NEWID())n2

关于SQL随机名称生成器不在同一行中插入名字和姓氏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31948717/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com