我试图构建一个似乎是一个常见的报告来向我展示我玩过哪些彩票的数字与一个或多个宣布的获胜抽奖相匹配。我有以下查询,它正是我想要的SQL提示。但是,我需要将其表示为 VIEW ,以便我的单个ODBC连接可以访问两个单独的数据库,但我无法弄清楚如何设置单独的子视图而不需要联合。有谁能告诉我一个更好的方法?指导赞赏。
SELECT record_id, GROUP_CONCAT(num ORDER BY num) nums
FROM (
SELECT record_id, n1 num FROM LA.tickets UNION
SELECT record_id, n2 num FROM LA.tickets UNION
SELECT record_id, n3 num FROM LA.tickets UNION
SELECT record_id, n4 num FROM LA.tickets UNION
SELECT record_id, n5 num FROM LA.tickets
) foo
WHERE num = ANY ((SELECT n1 n FROM L.draw WHERE record_id = 1) UNION
(SELECT n2 n FROM L.draw WHERE record_id = 1) UNION
(SELECT n3 n FROM L.draw WHERE record_id = 1) UNION
(SELECT n4 n FROM L.draw WHERE record_id = 1) UNION
(SELECT n5 n FROM L.draw WHERE record_id = 1)
)
GROUP BY record_id
HAVING count(*) >= 1;
(在这种情况下,' draw'表的记录#1包含最近绘制的5个数字。
答案 0 :(得分:0)
好吧,快速谷歌mysql create view
让我看到了http://dev.mysql.com/doc/refman/5.0/en/create-view.html,这给了我这样的语法(只需逐字复制/粘贴你的SELECT
语句):
CREATE VIEW foo AS
SELECT record_id, GROUP_CONCAT(num ORDER BY num) nums
FROM (
SELECT record_id, n1 num FROM LA.tickets UNION
SELECT record_id, n2 num FROM LA.tickets UNION
SELECT record_id, n3 num FROM LA.tickets UNION
SELECT record_id, n4 num FROM LA.tickets UNION
SELECT record_id, n5 num FROM LA.tickets
) foo
WHERE num = ANY ((SELECT n1 n FROM L.draw WHERE record_id = 1) UNION
(SELECT n2 n FROM L.draw WHERE record_id = 1) UNION
(SELECT n3 n FROM L.draw WHERE record_id = 1) UNION
(SELECT n4 n FROM L.draw WHERE record_id = 1) UNION
(SELECT n5 n FROM L.draw WHERE record_id = 1)
)
GROUP BY record_id
HAVING count(*) >= 1;