如何将一个选择查询中的值使用到另一个联合中?在线演示

问题描述 投票:0回答:1

我正在尝试将随机值组成的行插入表'Users'中,其中电子邮件取决于用户名的值:

上一个SELECT查询未看到“用户名”行(不存在)

我该如何解决?

SELECT(
  SELECT
  string_agg(substr(characters, (random() * length(characters) + 1)::integer, 1), '') as username
  from (values('abcdefghijklmnopqrstuvwxyz0123456789')) as symbols(characters) join generate_series(1, 15) on 1 = 1
  UNION
  SELECT
  string_agg(substr(characters, (random() * length(characters) + 1)::integer, 1), '') as password
  from (values('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789*')) as symbols(characters) join generate_series(1, 15) on 1 = 1
  UNION
  SELECT
  string_agg(substr(characters, (random() * length(characters) + 1)::integer, 1), '') as phonenumber
  from (values('0123456789')) as symbols(characters) join generate_series(1, 9) on 1 = 1
  UNION
  SELECT
    (username || '@' || (
    CASE (RANDOM() * 2)::INT
      WHEN 0 THEN 'gmail'
      WHEN 1 THEN 'hotmail'
      WHEN 2 THEN 'yahoo'
    END
  ) || '.com') AS email
    ) INTO "User" from generate_series(1,10000)

这也不起作用:

SELECT(
  SELECT
  string_agg(substr(characters, (random() * length(characters) + 1)::integer, 1), '') as username
  from (values('abcdefghijklmnopqrstuvwxyz0123456789')) as symbols(characters) join generate_series(1, 15) on 1 = 1
  UNION
  SELECT
  string_agg(substr(characters, (random() * length(characters) + 1)::integer, 1), '') as password
  from (values('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')) as symbols(characters) join generate_series(1, 15) on 1 = 1
  as select1
  UNION
  SELECT
  string_agg(substr(characters, (random() * length(characters) + 1)::integer, 1), '') as phonenumber
  from (values('0123456789')) as symbols(characters) join generate_series(1, 9) on 1 = 1
  UNION
  SELECT
    (select1.username || '@' || (
    CASE (RANDOM() * 2)::INT
      WHEN 0 THEN 'gmail'
      WHEN 1 THEN 'hotmail'
      WHEN 2 THEN 'yahoo'
    END
  ) || '.com') AS email
    ) INTO "User" from generate_series(1,10000)
sql postgresql
1个回答
1
投票

考虑递归CTE查询并删除顶级列的子查询:

WITH RECURSIVE main AS
   (
     SELECT 
         string_agg(substr(shortchars, (random() * length(shortchars) + 1)::integer, 1),'') AS "username"
       , string_agg(substr(longchars, (random() * length(longchars) + 1)::integer, 1), '') AS "password"
       , substr(string_agg(substr(nums, (random() * length(nums) + 1)::integer, 1), ''),1,9) AS "phonenumber"
       , 1 AS n

       FROM (values('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*')) as longchars(longchars) 
       CROSS JOIN (values('abcdefghijklmnopqrstuvwxyz0123456789')) as shortchars(shortchars)
       CROSS JOIN (values('0123456789')) as num(nums)
       CROSS JOIN generate_series(1, 15)

     UNION ALL

     SELECT 
         string_agg(substr(shortchars, (random() * length(shortchars) + 1)::integer, 1),'') AS "username"
       , string_agg(substr(longchars, (random() * length(longchars) + 1)::integer, 1), '') AS "password"
       , substr(string_agg(substr(nums, (random() * length(nums) + 1)::integer, 1), ''),1,9) AS "phonenumber"
       , n + 1

       FROM (values('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*')) as longchars(longchars) 
       CROSS JOIN (values('abcdefghijklmnopqrstuvwxyz0123456789')) as shortchars(shortchars)
       CROSS JOIN (values('0123456789')) as num(nums)
       CROSS JOIN generate_series(1, 15)
       CROSS JOIN (SELECT n FROM main LIMIT 1) AS mn

     WHERE n < 10
     GROUP BY mn.n
  )


SELECT CONCAT(main."username", 
           '@',
           ( CASE (RANDOM() * 2)::INT 
                  WHEN 0 THEN 'gmail'
                  WHEN 1 THEN 'hotmail' 
                  WHEN 2 THEN 'yahoo' 
              END ), 
           'com') AS email
     , main."username"
     , main."password"
     , main."phonenumber"

FROM main

Online Demo

热门问题
推荐问题
最新问题