CREATE VIEW WHERE SELECTid = VIEWrowID

泪湿孤枕 提交于 2019-12-13 01:33:36

问题


Warning: vagueness & unclear questioning will abound because I know squat about databases.

I just discovered that I need to use views as surrogates for a cronned update statement. I can somewhat get the view to work, but I'm having trouble with rows.

This post helped me to bang out the update I need, but now that I know that views can run that update whenever it's needed rather than on a cron schedule, how can I set the view's column value based upon the view's row id or equivalent?

I've got the select I need:

 SELECT SUM( table2.column1/ (

 SELECT table2constant
 FROM table3
 )
 FROM table2
 WHERE table2table1id = table1id

table1id is the AI id column for table1. table2table1id is PKd to table1id. I'd like the view to have a column PKd to table1id like with table2, and the view needs to have every distinct table1id represented.

I'm sure the jargon's way off, but hopefully you can see what I need.

Will provide as many edits as necessary for clarity.

Many thanks in advance!

EDIT1

Should I create a trigger that creates the view upon insert to table1? Just found about materialization which is what I need/want?

Clarity

I need a summed value for each table1.table1id

Progress

With this code, I'm getting the first id from table1 and only the total sum. I need a sum for each table1.id.

CREATE VIEW db1.sums as 

SELECT SUM( table2.column1/ (

 SELECT table2constant
 FROM table3
 ) as theSum, table1id
 FROM table1, table2
 WHERE table2.table2table1id = table1.table1id

回答1:


To be clear I'm still not sure what you're trying to accomplish here but if what you posted works, try

SELECT table1.table1id,
    SUM( table2.collumn1 ) / (SELECT table2constant FROM table3 ) as theSum
 FROM table1, table2
 WHERE table2.table2table1id = table1.table1id GROUP BY table1.table1id

you can replace (SELECT table2constant FROM table3 ) with your constant if it has no reason to otherwise be in the database (if it's not updated)




回答2:


Its actually very simple. Here is an example of how you can do it.

SELECT SUM( table1.column / table2.column ), table1.*, table2.*
FROM table1, table2 
WHERE table1.id = table2.column_id


来源:https://stackoverflow.com/questions/12761706/create-view-where-selectid-viewrowid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!