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/)