Results 1 to 6 of 6

Thread: [RESOLVED] Combining SQL statements

Hybrid View

  1. #1
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Combining SQL statements

    It is fine to run it as two separate statements, and is actually better that way.

    The reason for this is that you are presumably getting different amounts of rows from each query - the first returns one row (including "Instructions", which I guess is large), and the second returns multiple rows.

    If you merge them, you will get all of the data for the first query repeated in every row, along with the data from the second query. This extra data wastes memory and reduces speed - as well as increasing network traffic if your code/database are on different computers.

    The only time it might be worthwhile merging them is if the data returned by the first query is small, and/or there are only 1 or 2 rows returned by the second query. Unless you expect this to be the usual case, I wouldn't recommend doing it - what you have already is better for the majority of cases (perhaps even all).


    One thing that I don't like about your current method tho is your use of sub-queries for Category etc, you should be using joins instead as you do for recipeingredient (if you have the choice, preferably specify "Inner"/"Left"/etc, to make it easier to read).

  2. #2

    Thread Starter
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Combining SQL statements

    Thanks for the response si. I'm glad that was your answer.

    I used the subqueries because they seemed a bit easier to construct; particularly when I had multiple tables involved. If the joins will be more effective, then I'm definitely going to make the change.

  3. #3

    Thread Starter
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Combining SQL statements

    Actually, I take that back. I got myself all scared with joins on a bunch of tables and it actually was easy. Thanks again.

Posting Permissions

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



Click Here to Expand Forum to Full Width