MS Version of This MySQL View with GROUP BY?

2019-08-31 05:32发布

I was shown the ease that one can make a view from multiple tables, GROUPing BY an id of one of the tables in xception's awesome answer here: CREATE VIEW WHERE SELECTid = VIEWrowID

Is there any way to do that in MS? Everywhere, I've read says "no", but no page gives an alternative.

I don't need the counts or anything, just multiple columns from multiple tables GROUPed BY(?) a single column on one table.

Thanks a lot in advance!

EXAMPLE

Thank-you for responding.

For the view's SELECT:

SELECT dbo.table1.column1 AS table1column1,
       dbo.table1.column2 AS table1column2,
       dbo.table2.column1 AS table2column1,
       dbo.table2.column2 AS table2column2
FROM table1, table2
WHERE table2.column1 = table1.column1
GROUP BY table1.column1

1条回答
冷血范
2楼-- · 2019-08-31 06:25

As MySQL simply picks a random value from the non-grouped columns, the following should do it:

SELECT dbo.table1.column1 AS table1column1,
       min(dbo.table1.column2) AS table1column2,
       min(dbo.table2.column1) AS table2column1,
       min(dbo.table2.column2) AS table2column2
FROM table1, table2
WHERE table2.column1 = table1.column1
GROUP BY table1.column1

I highly recommend you read this blog posting http://rpbouman.blogspot.de/2007/05/debunking-group-by-myths.html to understand what MySQL is doing (wrong)

查看更多
登录 后发表回答