join - How can I select distinct column based on max datetime (another column) also joining two tables in MySQL? -
i've read many similar questions here , tried can think of, without success. think i've found question based on single table, or without need getting distinct column, not situation exactly.
i want distinct ticker_symbol , corresponding ticker_name , latest ticker_quote based on these tables:
create table `tickers` ( `id` int(10) unsigned not null auto_increment, `ticker_symbol` varchar(6) collate utf8_unicode_ci not null, `ticker_name` varchar(128) collate utf8_unicode_ci not null, `created_at` timestamp not null default '0000-00-00 00:00:00', `updated_at` timestamp not null default '0000-00-00 00:00:00', primary key (`id`) ) engine=innodb auto_increment=3 default charset=utf8 collate=utf8_unicode_ci create table `ticker_quotes` ( `id` int(10) unsigned not null auto_increment, `ticker_symbol` varchar(6) collate utf8_unicode_ci not null, `ticker_quote` float(8,2) not null, `created_at` timestamp not null default '0000-00-00 00:00:00', `updated_at` timestamp not null default '0000-00-00 00:00:00', primary key (`id`) ) engine=innodb auto_increment=111 default charset=utf8 collate=utf8_unicode_ci
try
select q.ticker_symbol, t.ticker_name, q.ticker_quote (select ticker_symbol, max(created_at) created_at ticker_quotes group ticker_symbol) m join ticker_quotes q on m.ticker_symbol = q.ticker_symbol , m.created_at = q.created_at join tickers t on q.ticker_symbol = t.ticker_symbol
Comments
Post a Comment