我有两个工作的MySQL视图:
第一个视图选择表portfolio
中的所有行,其中id也在表mural
中。
CREATE
ALGORITHM = UNDEFINED
DEFINER = `root`@`localhost`
SQL SECURITY DEFINER
VIEW `view_mural` AS
select
`portfolio`.`id` AS `id`,
`portfolio`.`customer_id` AS `customer_id`,
`portfolio`.`location_id` AS `location_id`,
`portfolio`.`title_text_id` AS `title_text_id`,
`portfolio`.`description_text_id` AS `description_text_id`,
`portfolio`.`started` AS `started`,
`portfolio`.`finished` AS `finished`,
from
`portfolio`
where
`portfolio`.`id` in (select
`mural`.`portfolio_id`
from
`mural`) WITH CASCADED CHECK OPTION
第二个视图从两个表中选择行:portfolio
和mural
。
CREATE
ALGORITHM = UNDEFINED
DEFINER = `root`@`localhost`
SQL SECURITY DEFINER
VIEW `view_mural` AS
select
`portfolio`.`id` AS `id`,
`portfolio`.`customer_id` AS `customer_id`,
`portfolio`.`location_id` AS `location_id`,
`portfolio`.`title_text_id` AS `title_text_id`,
`portfolio`.`description_text_id` AS `description_text_id`,
`portfolio`.`started` AS `started`,
`portfolio`.`finished` AS `finished`,
(select
`mural`.`width`
from
`mural`
where
`mural`.`portfolio_id`=`portfolio`.`id`) AS `mup`
from
`portfolio`
我无法得到,如何将这些组合成一个视图,选择表,两个表中的列,其中包含表组合中的行,其中id也在表壁画中。
当我添加:
where
`portfolio`.`id` in (select
`mural`.`portfolio_id`
from
`mural`) WITH CASCADED CHECK OPTION
到我的第二个视图的最后,它显示了一个错误:
Apply changes to view_mural Error 1368: CHECK OPTION on non-updatable view 'view_mural' SQL Statement: CREATE OR REPLACE ALGORITHM = UNDEFINED DEFINER = `root`@`localhost` SQL SECURITY DEFINER VIEW `view_mural` AS select `portfolio`.`id` AS `id`, `portfolio`.`customer_id` AS `customer_id`, `portfolio`.`location_id` AS `location_id`, `portfolio`.`title_text_id` AS `title_text_id`, `portfolio`.`description_text_id` AS `description_text_id`, `portfolio`.`started` AS `started`, `portfolio`.`finished` AS `finished`, (select `mural`.`width` from `mural` where `mural`.`portfolio_id`=`portfolio`.`id`) AS `mup` from `portfolio` where `portfolio`.`id` in (select `mural`.`portfolio_id` from `mural`) WITH CASCADED CHECK OPTION Error when running failback script. Details follow. Error 1050: Table 'view_mural' already exists SQL Statement: CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `view_mural` AS select `portfolio`.`id` AS `id`,`portfolio`.`customer_id` AS `customer_id`,`portfolio`.`location_id` AS `location_id`,`portfolio`.`title_text_id` AS `title_text_id`,`portfolio`.`description_text_id` AS `description_text_id`,`portfolio`.`started` AS `started`,`portfolio`.`finished` AS `finished`,(select `mural`.`width` from `mural` where ((`mural`.`portfolio_id` = `portfolio`.`id`) and `portfolio`.`id` in (select `mural`.`portfolio_id` from `mural`))) AS `mup` from `portfolio`
有人能指出我该怎么办?
答案 0 :(得分:0)
了解SQL joins:
SELECT
portfolio.id,
portfolio.customer_id,
portfolio.location_id,
portfolio.title_text_id,
portfolio.description_text_id,
portfolio.started,
portfolio.finished,
mural.width
FROM
portfolio JOIN mural ON mural.portfolio_id = portfolio.id