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: ...
| |||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| 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. |
| ||||
| 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). |