MySQL Query Containing Multiple Queries.

Discussion in 'MySQL' started by FishSword, Jan 16, 2011.

  1. #1
    Hiya,

    I have a database that holds basic "products", "users", and "sales" information for a shop.
    I am having a few problems with a MySQL query that will retrieve the following information from the appropriate tables.

    • The Purchasers "Username".
    • The "Name" of the purchased item.
    • The "Price" of the purchased item.
    • The "time" the item was purchased.
    If any items have been purchased multiple times by a purchaser, I need to only show the latest one. Can this be done?

    The structure of the tables can be seen below.

    Table "Products":

    • product_id - Used as a unique identifier.
    • name - The name of the product.
    • price - The price of the product.

    Table "Users":


    • user_id - Used as a unique identifier.
    • username - The username of the purchaser.

    Table "Sales":


    • sale_id - Used as a unique identifier.
    • product_id - The unique identifier of the purchased product.
    • user_id - The unique identifier of the purchaser.
    • time - The date and time the sale took place.
    Can anyone help?
    Any Help is Much Appreciated!

    Cheers ;)
     
    FishSword, Jan 16, 2011 IP
  2. jkl6

    jkl6 Peon

    Messages:
    70
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    SELECT username, name, price, time 
    FROM products p, users u, sales s
    WHERE p.product_id=s.product_id AND u.user_id=s.user_id
    ORDER BY time DESC LIMIT 1
    Code (markup):
     
    jkl6, Jan 16, 2011 IP
  3. FishSword

    FishSword Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    Thanks for this. Is it possible to display all items that have only been purchased once by a purchaser, along with the most recent item a purchaser has purchased if purchased multiple times.
    Also, would it be possible to display the most recent item in descending order of price, if an item has been purchased multiple times?
     
    Last edited: Jan 23, 2011
    FishSword, Jan 23, 2011 IP