Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Data Bases > Databases > [SQL] Do GROUP ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 1 Topic 376 of 383
Post > Topic >>

[SQL] Do GROUP BY return the last record inserted?

by Maciej Piechotka <uzytkownik2@[EMAIL PROTECTED] > Feb 13, 2008 at 11:39 PM

I have tables:

CREATE TABLE items (
       id INT PRIMARY KEY,
       some_value INT
);

CREATE TABLE item_histories (
       item_id INT REFERENCES items(id),
       name VARCHAR(255),
       created_at DATETIME UNIQUE
);

I would like to get a result which consist of id, some_value nad last name
from item_histories. Assuming that the created_at is the date of inserting
the
element into table is this SQL COMMAND sufficient to select them:

SELECT id, some_value, name FROM items LEFT JOIN item_histories
WHERE item_id = id GROUP BY id;

or it database-specific and I have to execute:
SELECT id, some_value, name FROM products LEFT JOIN item_histories
WHERE item_histories.item_id = items.id AND item_histories.created_at =
(SELECT id FROM item_histories ih WHERE ih.profuct_id = items.id
ORDER BY created_at DESC LIMIT 1);

Best regards
PS.
Although the creation may be not so universal (it is handled by ORM)
I would like to make the selection as universal ar possible since it will
be
only partialy encoded by ORM.
-- 
I've probably left my head... somewhere. Please wait untill I find it.
Homepage (pl_PL): http://uzytkownik.jogger.pl/
(GNU/)Linux User: #425935 (see http://counter.li.org/)




 1 Posts in Topic:
[SQL] Do GROUP BY return the last record inserted?
Maciej Piechotka <uzyt  2008-02-13 23:39:55 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan13V112 Fri Jul 4 22:28:21 CDT 2008.