我需要使用来自两个不同表格的数据创建一个视图。
我试过这个:
CREATE VIEW `moviesdatastore`.`view_user_group` AS
SELECT user_name FROM tbl_spj_user,
SELECT group_name FROM tbl_spj_group;
但这会导致:
ERROR 1349: View's SELECT contains a subquery in the FROM clause
看不到子查询在哪里.....
如何从两个不同的表中创建数据视图? 我在我的java Web应用程序上需要它用于JAAS。
答案 0 :(得分:1)
您可以使用union
CREATE VIEW `moviesdatastore`.`view_user_group` AS
(
SELECT user_name AS name FROM tbl_spj_user
)
UNION ALL
(
SELECT group_name AS name FROM tbl_spj_group;
)
请注意,您不会知道哪些条目来自组,哪些条目来自用户。如果您需要对其进行区分,请使用类型向视图添加另一列,例如:
CREATE VIEW `moviesdatastore`.`view_user_group` AS
(
SELECT user_name AS name, 'user' AS type FROM tbl_spj_user
)
UNION ALL
(
SELECT group_name AS name, 'group' AS type FROM tbl_spj_group;
)
答案 1 :(得分:1)
如果你可以加入表格,那么加入应该比联盟更好
CREATE VIEW `moviesdatastore`.`view_user_group` AS
SELECT
user_name
FROM tbl_spj_user
INNER JOIN tbl_spj_group ON tbl_spj_group.groupID=tbl_spj_user.groupID
答案 2 :(得分:1)
可能是你想要的连接。
CREATE VIEW `moviesdatastore`.`view_user_group` AS
SELECT A.user_name, B.group_name
FROM tbl_spj_user A
LEFT JOIN tbl_spj_group B ON A.group_id = B.id;