php - Multiple selection of three tables -


i have 3 linked tables - order_tab contains orders, autosorder_tab connects 2 other , auto_tab contains cars in shop.
1 order can contain number of cars.

create table order_tab (`id` int, `_phone` varchar(10), `_email` varchar(9), `_date` varchar(10)) ;  insert order_tab (`id`, `_phone`, `_email`, `_date`) values (1, '111-111111', 'ok@ok.com', '2013-08-19') ;  create table auto_tab (`id` int, `us_id` int, `str_1` varchar(3), `str_2` varchar(8)) ;  insert auto_tab (`id`, `us_id`, `str_1`, `str_2`) values (2, 0, 'bmw', '530i e60'), (6, 0, 'bmw', '530i') ;  create table autosorder_tab (`id` int, `au_id` int, `or_id` int, `hours` int, `price` decimal(19,2)) ;  insert autosorder_tab (`id`, `au_id`, `or_id`, `hours`, `price`) values (1, 2, 1, 3, 2700), (2, 6, 1, 2, 3500) 

order_tab id - main. or_id autosorder_tab id order_tab. au_id id auto_tab
how can select orders cars in each order?

if understand correctly use join

select o.*, a.*   autosorder_tab ao join order_tab o     on ao.or_id = o.id join auto_tab     on ao.au_id = a.id  order o.id, a.id 

here sqlfiddle demo

update if want group information per order use group by , appropriate aggregate functions. names of cars want use group_concat()

select o.id, o._date,         group_concat(concat(a.str_1, ' ', a.str_2)            order a.str_1, a.str_2           separator ',') autos,        sum(hours) hours,        ...   autosorder_tab ao join order_tab o     on ao.or_id = o.id join auto_tab     on ao.au_id = a.id  group o.id 

to change delimiter use separator clause. or use comma default , when iterating on result set in client code change according presentation rules.

here sqlfiddle demo


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -