I have some question about create Mysql
view.
Table:
ID name num
1 ccc 3
1 bbb 4
1 aaa 2
query:"SELECT ID,name,num FROM (SELECT * FROM test ORDER BY ID,num DESC) AS A GROUP BY ID;"
Result:
1 bbb 4
This result is what I want.
query:
"CREATE VIEW I AS SELECT * FROM test ORDER BY ID,num DESC,`name`;
CREATE VIEW T AS SELECT ID,name,num FROM I GROUP BY ID;"
VIEW T:
1 aaa 2
How can I make VIEW T is same as query result?
答案 0 :(得分:0)
One way to phrase this query is to join to a subquery which finds the maximum number values for each ID
:
CREATE VIEW T AS
SELECT
t1.ID, t1.name, t1.num
FROM test t1
INNER JOIN
(
SELECT ID, MAX(num) AS max_num
FROM test
GROUP BY ID
) t2
ON t1.ID = t2.ID AND
t1.num = t2.max_num
答案 1 :(得分:0)
Well use the same select
query in your view statement
create view T as
SELECT ID,name,num
FROM (SELECT * FROM test ORDER BY ID,num DESC) AS A
GROUP BY ID;
Finally, you can't create view from a view and thus the below statement is invalid
create view T as
select * from I; --I is a view