Ask a Question related to MySQL, Design and Development.
-
André Hänsel #1
Avoiding subquery
Hi,
if anyone feels like a little quiz (but I don't know the answer), just for
interest:
Is it possible to rewrite this:
SELECT domain,ftptraffic,(SELECT SUM(traffic) FROM monthlytraffic WHERE year
= '2005' AND monthlytraffic.domain = totaltraffic.domain) AS correction FROM
totaltraffic
or even this:
UPDATE totaltraffic SET ftptraffic = ftptraffic - (SELECT SUM(traffic) FROM
monthlytraffic WHERE year = '2005' AND monthlytraffic.domain =
totaltraffic.domain)
without using subqueries?
Since I use MySQL > 4.1 it works fine this way... I'm amazed. ;)
Regards,
André
André Hänsel Guest
-
Subquery
I am trying to update a table with data in a "look up table." Specifically, I have a table that contains zip codes but is missing both city &... -
using list in subquery
I have a query that I would like to pass a list of values to use in the WHERE statement. How do I use a list in the WHERE statement? Example:... -
SQL subquery question
I have a query running inside of Coldfusion webpage. query blah cfoutput query blah { query another_blah { cfoutput another_blah -
Select From SubQuery
Yes you can create pseudo tables and reference them in the outer query as you have done. -- HTH, SriSamp Please reply to the whole group only! ... -
subquery with more than 1 fields
select * from ordermaster where (orderid,customerid) in select orderid,customerid from orderdetail the sql can't be run in ms sql and is there... -
Bill Karwin #2
Re: Avoiding subquery
"André Hänsel" <andre@webkr.de> wrote in message
news:dt3672$7ou$1@sagnix.uni-muenster.de...Ok, I'll give it a shot. Note that I haven't tested this, so caveat emptor.> Is it possible to rewrite this:
>
> SELECT domain,ftptraffic,(SELECT SUM(traffic) FROM monthlytraffic WHERE
> year
> = '2005' AND monthlytraffic.domain = totaltraffic.domain) AS correction
> FROM
> totaltraffic
:-)
SELECT t.domain, t.ftptraffic, SUM(m.traffic) AS correction
FROM totaltraffic AS t LEFT OUTER JOIN monthlytraffic AS m
ON m.domain = t.domain AND m.year = '2005'
By using the outer join, this delivers all the rows in totaltraffic, even if
there was no traffic for a given domain during 2005. So it should give the
same results as your query.
UPDATE totaltraffic AS t, monthlytraffic AS m> or even this:
>
> UPDATE totaltraffic SET ftptraffic = ftptraffic - (SELECT SUM(traffic)
> FROM
> monthlytraffic WHERE year = '2005' AND monthlytraffic.domain =
> totaltraffic.domain)
SET t.ftptraffic = t.ftptraffic - m.traffic
WHERE m.domain = t.domain AND m.year = '2005'
You don't have to do the SUM this way. It does the subtraction for each
domain/month combination. But the end result is the same. This uses the
MySQL extended syntax for multi-table update, which is not standard SQL.
Regards,
Bill K.
Bill Karwin Guest
-
André Hänsel #3
Re: Avoiding subquery
Bill Karwin wrote:
I didn't yet try to understand your variant, but it gives me the "Mixing of> "André Hänsel" <andre@webkr.de> wrote in message
> news:dt3672$7ou$1@sagnix.uni-muenster.de...>>> Is it possible to rewrite this:
>>
>> SELECT domain,ftptraffic,(SELECT SUM(traffic) FROM monthlytraffic
>> WHERE year
>> = '2005' AND monthlytraffic.domain = totaltraffic.domain) AS
>> correction FROM
>> totaltraffic
> Ok, I'll give it a shot. Note that I haven't tested this, so caveat
> emptor. :-)
>
> SELECT t.domain, t.ftptraffic, SUM(m.traffic) AS correction
> FROM totaltraffic AS t LEFT OUTER JOIN monthlytraffic AS m
> ON m.domain = t.domain AND m.year = '2005'
>
> By using the outer join, this delivers all the rows in totaltraffic,
> even if there was no traffic for a given domain during 2005. So it
> should give the same results as your query.
group comlumns with non group columns is illegal withour GROUP BY"-error.
André Hänsel Guest
-
Bill Karwin #4
Re: Avoiding subquery
"André Hänsel" <andre@webkr.de> wrote in message
news:dt3bg5$8qk$1@sagnix.uni-muenster.de...Woops!> I didn't yet try to understand your variant, but it gives me the "Mixing>> SELECT t.domain, t.ftptraffic, SUM(m.traffic) AS correction
>> FROM totaltraffic AS t LEFT OUTER JOIN monthlytraffic AS m
>> ON m.domain = t.domain AND m.year = '2005'
>>
> of
> group comlumns with non group columns is illegal withour GROUP BY"-error.
SELECT t.domain, t.ftptraffic, SUM(m.traffic) AS correction
FROM totaltraffic AS t LEFT OUTER JOIN monthlytraffic AS m
ON m.domain = t.domain AND m.year = '2005'
GROUP BY t.domain
I tried this (though with no data) and at least it doesn't give the error
about mixing of group columns.
Regards,
Bill K.
Bill Karwin Guest



Reply With Quote

