|
|
|||||||||
|
|||||||||
|
|||||||||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
Use value from select in same query
Hello,
I want to do something like this UPDATE mytable m SET m.value = (SELECT SUM(l.val) FROM list l WHERE m.id = l.id) * 3; I've tried this, but it doesn't work. I thought about using GROUP BY, but I am not so sure how that will help. Can someone help me? Thank you. |
|
#2
|
||||
|
||||
|
try this:
Code:
update mytable set value = strUpdate.sum_Value from mytable m inner join (select id, sum(val) as sum_Value from list group by id) as strUpdate on m.id = strUpdate.id
__________________
Come JOIN the party!!! Quote of the Month: Trouble: Luck can't last a lifetime unless you die young. Questions to Ponder: Do cemetery workers prefer the graveyard shift? iif([sarcasm]=true,iif([you have to ask]=true,"didn't work","ha ha ha"),"not sarcasm") copyright© 2008 sbenj69 |
|
#3
|
|||
|
|||
|
Quote:
I think I still need help :| This is my complete query (of course, with your suggestion) Code:
UPDATE links SET ratio = (incl.cSUM + 1.0) / (outcl.cSUM + 1.0) FROM links l INNER JOIN (SELECT link, SUM(cnt) AS cSUM FROM inclicks GROUP BY link) AS incl on l.id = incl.link INNER JOIN (SELECT link, SUM(cnt) AS cSUM FROM outclicks GROUP BY link) AS outcl on l.id = outcl.link; And when I try to start that I am getting message "MySQL][ODBC 3.51 Driver][mysqld-5.0.45-log]You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM links l INNER JOIN (SELECT link, SUM(cnt) AS cSUM FROM inclicks GROUP BY li' at line 1" I was looking on net for tutorial about inner join and I have found (and understand) I will get temporary table that contains 'links', 'inclicks' and 'outlicks' (in my case these tables) and one row that correspond to l.id. But how ratio knows which row to take? So, with join I should have l.id | incl.cSUM | outcl.cSum ----------------------------- 1 | 10 | 20 ----------------------------- 2 | 3 | 15 ----------------------------- 5 | 8 | 33 ----------------------------- But how ratio for id = 5 will know that it is equal to 9 / 34. I believe that should work, but I don't understand why. I will understand if there is something like this UPDATE links l1 SET [other part of query] WHERE l.id = l1.id; but there is not. Thank you. |
![]() |
| Viewing: ASP Free Forums > Database > SQL Development > Use value from select in same query |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|
|
|
|