Коллеги. Помогите с запросом. Что-то застрял.
MySQL версия без subqueries надо передалать, но что-бы был один
SELECT p.PNID, p.Store, p.Mfg, i.Time, i.ChangeType, i.QtyChange
FROM p, i WHERE p.PNID = i.PNID AND
i.Time = (SELECT max(i2.Time) FROM i i2 WHERE p.PNID = i2.PNID)
Пиво гарантирую.
Subquery -> Join
-
- Уже с Приветом
- Posts: 2278
- Joined: 02 Jan 2001 10:01
- Location: MSK; NJ; MA; UAE, Chicago
Subquery -> Join
Гриша
------------
Why would anybody come here if they had a pony? Who leaves a country packed with ponies to come to a non-pony country? It doesn't make sense.. am I wrong?
------------
Why would anybody come here if they had a pony? Who leaves a country packed with ponies to come to a non-pony country? It doesn't make sense.. am I wrong?
-
- Уже с Приветом
- Posts: 664
- Joined: 05 Jun 2002 01:11
Re: Subquery -> Join
GShapiev wrote:Коллеги. Помогите с запросом. Что-то застрял.
MySQL версия без subqueries надо передалать, но что-бы был один
SELECT p.PNID, p.Store, p.Mfg, i.Time, i.ChangeType, i.QtyChange
FROM p, i WHERE p.PNID = i.PNID AND
i.Time = (SELECT max(i2.Time) FROM i i2 WHERE p.PNID = i2.PNID)
Пиво гарантирую.
You can re-write the query as a three-way join so:
Code: Select all
select p.PNID,
p.Store,
p.Mfg,
i.Time,
i.ChangeType,
i.QtyChange
from
p,
i,
(select pnid, max(time) time from i
group by pnid
) i2
where p.pnid=i.pnid and i.pnid=i2.pnid and i.time=i2.time;
If MySQL does not support subqueries at all, you'll need to create a temporary table:
Code: Select all
create table i2 as select pnid, max(time) time from i
group by pnid;
... or whatever syntax MySQL uses.
... and then do the join:
Code: Select all
select p.PNID,
p.Store,
p.Mfg,
i.Time,
i.ChangeType,
i.QtyChange
from
p,
i,
i2
where p.pnid=i.pnid and i.pnid=i2.pnid and i.time=i2.time
VC
-
- Уже с Приветом
- Posts: 394
- Joined: 12 Feb 2001 10:01
- Location: USA
Re: Subquery -> Join
А если вот так
Code: Select all
SELECT p.PNID, p.Store, p.Mfg, i.Time, i.ChangeType, i.QtyChange, max(i2.Time)
FROM p, i, i i2
WHERE p.PNID = i.PNID AND p.PNID = i2.PNID
GROUP BY p.PNID, p.Store, p.Mfg, i.Time, i.ChangeType, i.QtyChange
HAVING i.Time = max(i2.Time)
-
- Уже с Приветом
- Posts: 2278
- Joined: 02 Jan 2001 10:01
- Location: MSK; NJ; MA; UAE, Chicago
Re: Subquery -> Join
Yuri_p33 wrote:А если вот такCode: Select all
SELECT p.PNID, p.Store, p.Mfg, i.Time, i.ChangeType, i.QtyChange, max(i2.Time)
FROM p, i, i i2
WHERE p.PNID = i.PNID AND p.PNID = i2.PNID
GROUP BY p.PNID, p.Store, p.Mfg, i.Time, i.ChangeType, i.QtyChange
HAVING i.Time = max(i2.Time)
супер. спасибо. совсем забыл про HAVING
Гриша
------------
Why would anybody come here if they had a pony? Who leaves a country packed with ponies to come to a non-pony country? It doesn't make sense.. am I wrong?
------------
Why would anybody come here if they had a pony? Who leaves a country packed with ponies to come to a non-pony country? It doesn't make sense.. am I wrong?
-
- Уже с Приветом
- Posts: 8249
- Joined: 23 Jul 2003 03:53
- Location: SPb - KW - NY - CT - MD
Re: Subquery -> Join
...del