Mulitple Table Query Help

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default Mulitple Table Query Help

    I'm not sure the follow multiple table query is the right way to do
    what I need to do although it seems to be working:

    $php_SQL = "SELECT * ".
    "FROM basics, personal, photos ".
    "WHERE basics.member_name = personal.member_name ".
    "AND basics.member_name = photos.member_name ".
    "AND basics.account_creation_date >= DATE_SUB(NOW(),
    INTERVAL 30 DAY)";

    I primarily need to return a resultset for all member_names (they are
    index key and unique) filtered for the last 30 days on the
    basics.account_creation_date - this 30 day thing is working fine.

    I need to access various other table fields and display this data on
    the web page - member_name is the common key for all tables - this all
    seems to be working fine as I have 8 test records and can manually
    track and see that it is working - the problem is that I need to add a
    4th table and in doing so, the resultset breaks and returns just one
    record - I am adding an online table with a field called is_online
    which is set to 'yes' if the member is online but the following query
    returns just the one record of the online member:

    $php_SQL = "SELECT * ".
    "FROM basics, personal, photos, online ".
    "WHERE basics.member_name = personal.member_name ".
    "AND basics.member_name = photos.member_name ".
    "AND basics.member_name = online.member_name ".
    "AND basics.account_creation_date >= DATE_SUB(NOW(),
    INTERVAL 30 DAY)";

    It seems to me this should be an easy thing to just add the 4th table
    but since this breaks the query now I'm wondering if this query
    structure is even built right?

    Any help would be greatly appreciated...

    Ralph Freshour Guest

  2. Similar Questions and Discussions

    1. Query w/bad table structure
      Hi, I am trying to query a column that has multiple values. When I export it, I need to have it so that each of those values is placed in its own...
    2. 2 table query problem
      Im having trouble with this query. I have 2 tables. I only want to display the events table but I want to sort the results based on the users...
    3. How do I Query mulitple datasources?
      Here is what I have and what I need to do... 1. I have multiple dabases with financial transactions. All are based on the same table structure. ...
    4. mulitple query insert problem
      Ben. I am doing the scenario where the id key is auto generated from Access. I know I need to get that id to do the second insert into the...
    5. ASP / Access: use of same table twice in one query
      Hi, In a query I use the same table twice: Select u.*, ud.*, dep.*, u2.* from users as u, departments as dep, user_in_departments as ud, users...
  3. #2

    Default Re: Mulitple Table Query Help

    Thanks for the comments -

    1. Yes I figured out that my online table was wrong, it needed all
    member_names in it - as you pointed out - that was indeed why it was
    resulting in one row - so I got that fixed and it works as expected
    now. Previously I was using that online table in another way and
    changed the way I use it and didn't realize it needed all the other
    member names in it.

    2. Yes, I wasn't sure about my post being in here - sometimes help is
    given in php (although not in this case) on mysql issues - but thanks
    for the comment and thanks for the table help.


    On Wed, 03 Sep 2003 02:05:44 +0200, Bruno Desthuilliers
    <bdesth.nospam@removeme.free.fr> wrote:
    >Ralph Freshour wrote:
    >> I'm not sure the follow multiple table query is the right way to do
    >> what I need to do although it seems to be working:
    >>
    >> $php_SQL = "SELECT * ".
    >> "FROM basics, personal, photos ".
    >> "WHERE basics.member_name = personal.member_name ".
    >> "AND basics.member_name = photos.member_name ".
    >> "AND basics.account_creation_date >= DATE_SUB(NOW(),
    >> INTERVAL 30 DAY)";
    >>
    >> I primarily need to return a resultset for all member_names (they are
    >> index key and unique)
    >> filtered for the last 30 days on the
    >> basics.account_creation_date - this 30 day thing is working fine.
    >>
    >> I need to access various other table fields and display this data on
    >> the web page - member_name is the common key for all tables - this all
    >> seems to be working fine as I have 8 test records and can manually
    >> track and see that it is working - the problem is that I need to add a
    >> 4th table and in doing so, the resultset breaks and returns just one
    >> record - I am adding an online table with a field called is_online
    >> which is set to 'yes' if the member is online but the following query
    >> returns just the one record of the online member:
    >>
    >> $php_SQL = "SELECT * ".
    >> "FROM basics, personal, photos, online ".
    >> "WHERE basics.member_name = personal.member_name ".
    >> "AND basics.member_name = photos.member_name ".
    >> "AND basics.member_name = online.member_name ".
    >
    ><OT>
    >This line above tells the DB to inner-join basics and online on the
    >member_name field. So if there is only one record in online, it is quite
    >normal that you only get this record in your resultset.
    >
    >I guess that you've got corresponding records in personal and photos for
    >all records in basics, else you would have spot the problem sooner.
    >
    >Now the result may not be what you expect, but as we dont know for sure
    >what you expect, it's hard to tell you how you could get it.
    >
    >BTW, could it be possible that there is a little mistake in your db schema ?
    >
    >You state that :
    >
    >> I am adding an online table with a field called is_online
    >> which is set to 'yes' if the member is online but the following query
    >> returns just the one record of the online member:
    >
    >If your schema is something like :
    >basics(*member_name*, ...)
    >online(*member_name*, yes_no)
    >
    >then it's broken. You should have a field 'online' in basics, and no
    >online table table.
    >
    >Or did I miss something ?
    ></OT>
    >
    >Oh, and BTW, your question is a bit off-topic here, since it has nothing
    >to do with php !-)
    >
    >comp.databases would have been a better place IMHO.
    >
    >Bruno
    Ralph Freshour Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139