Results 1 to 5 of 5

Thread: Best way to perform this task

  1. #1
    Addicted Member
    Join Date
    May 06
    Posts
    137

    Best way to perform this task

    So I have 2 tables:

    transaction:
    t_id
    t_amount
    t_cat
    t_user

    categories
    c_id

    So I call the list of 'transactions' with SELECT * FROM transactions WHERE t_user = 'userid'.

    This could load say 100 transactions.
    There are 500 records in 'categories'.

    The list of transactions could be across say 25 categories (therefore being multiple transactions for each category).

    For each category applicable I want to calculate the total amount in value from t_amount.

    What is the best way to do this?

    The only thing I can think of is to load all the 'categories' then perform a loop for each category to get all transactions with that category ID and adding the value.

    But this seems like it could be a very long process going through all the categories for just the few the user is using.........

  2. #2
    Hyperactive Member xxarmoxx's Avatar
    Join Date
    Mar 07
    Posts
    365

    Re: Best way to perform this task

    You always want to pull the exact data you need from the DB. Don't use PHP to loop through the transactions. How is a transaction linked to a category? You are going to want to join the tables.

  3. #3
    Junior Member
    Join Date
    Jun 12
    Posts
    17

    Re: Best way to perform this task

    why dont you use the concepts of joins here so that the query will be easy... and can get your results...
    Regards
    czns


    there is nothing impossible.....

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 02
    Posts
    21,636

    Re: Best way to perform this task

    I've asked the mods to move this as it's a Database question.

    And the others are right... what you need to do is join the two tables together... depending on the results you want, it can be either an inner join - which give you only the rows that exist in both tables - or a left join - which would give you all the rows in the left table, plus any that match in the right table.


    so, something like this:
    Code:
    select c.c_id, sum(t.t_amount) as Total
    from categories c
    inner join transaction t on c_id = t_cat
    group by c.c_id

    How ever, I STRONGLY suggest you pick another table name for your Transaction table, as Transaction really is a reserved word and can cause problems for you later....


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
    *Proof positive that searching the forums does work: View Thread *
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *
    * Use Offensive Programming, not Defensive Programming. * On Error Resume Next is error ignoring, not error handling(tm).
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN

  5. #5
    Super Moderator Hack's Avatar
    Join Date
    Aug 01
    Location
    Searching for mendhak
    Posts
    58,283

    Re: Best way to perform this task

    Quote Originally Posted by techgnome View Post
    I've asked the mods to move this as it's a Database question.
    And we have listened......moved to the database development section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •