SQL select all items of an owner from an item-to-owner table

Posted by kdobrev on Stack Overflow See other posts from Stack Overflow or by kdobrev
Published on 2010-06-03T15:45:13Z Indexed on 2010/06/03 15:54 UTC
Read the original article Hit count: 216

Filed under:
|
|

I have a table bike_to_owner. I would like to select current items owned by a specific user.

Table structure is CREATE TABLE IF NOT EXISTS `bike_to_owner` (
  `bike_id` int(10) unsigned NOT NULL,
  `user_id` int(10) unsigned NOT NULL,
  `last_change_date` date NOT NULL,
  PRIMARY KEY  (`bike_id`,`user_id`,`last_change_date`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

In the profile page of the user I would like to display all his/her current possessions.

I wrote this statement:

SELECT `bike_id`,`user_id`,max(last_change_date) FROM `bike_to_owner` 
WHERE `user_id` = 3 group by `last_change_date`

but i'm not quite sure it works correctly in all cases. Can you please verify this is correct and if not suggest me something better. Using php/mysql.

Thanks in advance!

© Stack Overflow or respective owner

Related posts about mysql

Related posts about select