使用行数不同的两个 select 查询插入数据
Insert data using two select queries with different count of rows
我希望在从前两个 tables.In 获取数据的同时将数据输入第三个 table 这种情况下,第一个 table 是 profile,第二个table是menu,第三个table是profile_menu。
我的 objective 是将菜单映射到特定的 profiles.Practically 一个配置文件可以映射到多个菜单。
我正在使用以下查询:
INSERT INTO profile_menu(item_id,profile_id) (
Select (select id_profile from profile where name = 'XYZ' and site = 46) as profile,
(SELECT m.id_menu_item FROM menu m WHERE names IN ('menu1'
,'menu1'
,'menu2'
,'menu3'
,'menu4'
,'menu5')) as menus
from dual);
虽然 运行 我收到“01427.00000 - "single-row subquery returns more than one row"”这个错误。
我知道我需要 select 个人资料中的相同行,但我不知道如何实现。
如果有人能帮忙。
谢谢。
如果我理解,请使用 join
:
select p.id_profile, m.id_menu_item
from profile p cross join
menu m
where p.name = 'XYZ' and p.site = 46 and
m.name in ('menu1' ,'menu1' ,'menu2' ,'menu3' ,'menu4' ,'menu5');
这将 return 多行。这大概就是你想要的。
我希望在从前两个 tables.In 获取数据的同时将数据输入第三个 table 这种情况下,第一个 table 是 profile,第二个table是menu,第三个table是profile_menu。 我的 objective 是将菜单映射到特定的 profiles.Practically 一个配置文件可以映射到多个菜单。 我正在使用以下查询:
INSERT INTO profile_menu(item_id,profile_id) (
Select (select id_profile from profile where name = 'XYZ' and site = 46) as profile,
(SELECT m.id_menu_item FROM menu m WHERE names IN ('menu1'
,'menu1'
,'menu2'
,'menu3'
,'menu4'
,'menu5')) as menus
from dual);
虽然 运行 我收到“01427.00000 - "single-row subquery returns more than one row"”这个错误。 我知道我需要 select 个人资料中的相同行,但我不知道如何实现。
如果有人能帮忙。
谢谢。
如果我理解,请使用 join
:
select p.id_profile, m.id_menu_item
from profile p cross join
menu m
where p.name = 'XYZ' and p.site = 46 and
m.name in ('menu1' ,'menu1' ,'menu2' ,'menu3' ,'menu4' ,'menu5');
这将 return 多行。这大概就是你想要的。