简体   繁体   中英

mysql, get total weight of products from based on UUID of category

I am attempting to aquire the total weight of gear, and the name of the category, based on the category that the gear is listed within.

I have three tables of concern.

'gear' (which holds the information about each piece of gear, including the weight) and it has a UUID and the weight field is called "Weight"

'gear_list' (which holds the name of the category in a field called "Subject" and has its own UUID)

'gear_list_items' (which contains the GearUUID, GearListUUID, CategoryUUID)

What I am able to pass to the php script is the GearListUUID.

So what I am needing here is a way to query and have results such as this:

CategoryName, 268 (weight/grams) AnotherCategoryName, 485 (weight/grams) AnotherCategoryName, 1028 (weight/grams) AnotherCategoryName, 768 (weight/grams) AnotherCategoryName, 448 (weight/grams)

I suspect I may need to post some schema dump, but if anybody has knowledge of how to do this before I go and post all that, it would be highly appreciated.

It's hard to tell for sure without seeing actual table schemas and sample data but you want something like

SELECT l.Subject, SUM(g.Weight) Weight
  FROM gear_list l JOIN gear_list_items i
    ON l.GearListUUID = i.GearListUUID JOIN gear g
    ON i.GearUUID = g.GearUUID
-- WHERE l. GearListUUID = ?
 GROUP BY l.Subject

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM