Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.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 如何将查询中的数据与数据列表连接起来?_Sql_Postgresql_Join - Fatal编程技术网

Sql 如何将查询中的数据与数据列表连接起来?

Sql 如何将查询中的数据与数据列表连接起来?,sql,postgresql,join,Sql,Postgresql,Join,第二个表中有两个表,其中包含一些数据 CREATE TABLE IF NOT EXISTS dst ( id integer NOT NULL, content varchar(200) NOT NULL); CREATE TABLE IF NOT EXISTS src ( id integer NOT NULL, name varchar(200) NOT NULL); INSERT INTO src VALUES('100', 'ET'), ('200', 'Luke')

第二个表中有两个表,其中包含一些数据

CREATE TABLE IF NOT EXISTS dst (
  id integer NOT NULL,
  content varchar(200) NOT NULL);

CREATE TABLE IF NOT EXISTS src (
  id integer NOT NULL,
  name varchar(200) NOT NULL);

INSERT INTO src VALUES('100', 'ET'), ('200', 'Luke'), ('300', 'Jojo');
现在,我从src插入一个选定的id和一些其他值列表。所以结果应该是这样的:

200, one
200, two
200, three
这是我的尝试

INSERT INTO dst (id, content) 
SELECT * FROM ((SELECT id FROM src WHERE name = 'Luke') AS bar 
CROSS JOIN ('one', 'two', 'three')) AS foo;

问题是我如何放置JOIN的第二个参数-('1','2','3'),因为这种方法不起作用<代码>错误:在“'one'”或其附近出现语法错误使用
UNION ALL
(在您的情况下,您没有将第二个子查询的列别名。例如,
作为内容
,逗号分隔的字符串
('one'、'two'、'three')
将不起作用,应转换为查询)


使用
UNION ALL
(在您的情况下,您没有将第二个子查询的列别名。例如,
作为内容
,逗号分隔的字符串
('one','two','three')
将不起作用,应转换为查询)


另一种解决方案是使用
unest()
-这将把数组转换成行:

select
    unnest(array['one', 'two', 'three']::varchar[]) as content
会让步

content (varchar)
---------------
one
two
three
在select语句中使用时,它隐式交叉连接:

insert into dst (
    id,
    content
) (
    select
        src.id,
        unnest(array['one', 'two', 'three']::varchar[])
    from
        src
    where
        src.name = 'Luke'
);

另一种解决方案是使用
unest()
-这将把数组转换成行:

select
    unnest(array['one', 'two', 'three']::varchar[]) as content
会让步

content (varchar)
---------------
one
two
three
在select语句中使用时,它隐式交叉连接:

insert into dst (
    id,
    content
) (
    select
        src.id,
        unnest(array['one', 'two', 'three']::varchar[])
    from
        src
    where
        src.name = 'Luke'
);

非常感谢。这不是很漂亮
选择'two'UNION ALL
,但我想这是唯一的办法。谢谢!这不是很漂亮
选择'two'UNION ALL
,但我想这是唯一的办法。