Unix Technical Forum

how to join three table into a little complicated query

This is a discussion on how to join three table into a little complicated query within the MySQL forums, part of the Database Server Software category; --> I am using version 4.1.22. I simplifying this for example's sake. I have three tables: order: order_id tip_amount item: ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 10:59 AM
Paul
 
Posts: n/a
Default how to join three table into a little complicated query

I am using version 4.1.22. I simplifying this for example's sake. I have
three tables:

order:
order_id
tip_amount

item:
item_id
order_id
price
vendor_id

vendor:
vendor_id
vendor Name

Each order can have many items. Each item has one vendor.

I need a query that displays all orders grouped by order_id, EXCEPT where
there are multiple vendors where I need it to show grouped by order_id in
the first column then grouped by vendor_id in the second column. Plus I need
the tip_amount to show ONLY on the first line of the order.

It would like this (notice order# 299 has two entries and the tip amount was
added to the first record vendor record that order):

order_id vendor_name total tip298 ABC $30
$2299 DEF $25 $4299 XYZ $12
300 LKW $17 $7...


Any ideas how to create such a query? Many thanks for your help.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 10:59 AM
=?iso-8859-1?B?QW5kcukgSORuc2Vs?=
 
Posts: n/a
Default Re: how to join three table into a little complicated query

On 8 Jun., 20:25, "Paul" <l...@invalid.com> wrote:
> I need a query that displays all orders grouped by order_id, EXCEPT where
> there are multiple vendors where I need it to show grouped by order_id in
> the first column then grouped by vendor_id in the second column. Plus I need
> the tip_amount to show ONLY on the first line of the order.


You can join and group your records quite straightforward, but you
will find that the values of the columns you grouped by are duplicated
in every row (same order_id for every vendor). You can then use a
procedural language to remove the spare values by comparing it to the
last line and only displaying it when the value hast changed.

An ugly possibility to do it in pure MySQL could be to use CASE to
only show the value when some value of the row is equal to the maximum
value of the column you grouped by (which means it is the first row of
the group).

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:57 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com