Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Posts
    104
    Thanks
    13
    Thanked 0 Times in 0 Posts

    php - Mysql Quering two tables

    Hello Guys..

    I have two mysql tables one called "ads" and the other called "ads_photo" and what they have in common is the ad_id.

    I need to be able to query both tables (maybe a left join?) and grab the top 8 - 10 profile_photos out based on the date.

    The columns
    Table - ads

    ad_id, ad_date
    --------------------------------
    Table - ads_photo

    profile_photo


    Just need some help..Thanks!

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    Did you supply all columns of both tables? There should be some kind of Primary/Foreign key relation tying the photos to the ad_id.

    And, yes, a JOIN query would be perfect for this.

    BTW.. this is really more of a SQL question than a PHP question. Just sayin'.
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #3
    Regular Coder
    Join Date
    Nov 2002
    Posts
    104
    Thanks
    13
    Thanked 0 Times in 0 Posts
    Thanks for the comment wolf.. The primary key is the ad_id

    The reason I posted this in PHP is because I will have to do some type of loop..

  • #4
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    Ah, understood. If the PK is ads.ad_id, what is the FK in ads_photo?
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #5
    Regular Coder
    Join Date
    Nov 2002
    Posts
    104
    Thanks
    13
    Thanked 0 Times in 0 Posts
    Thats correct...

  • #6
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    What is correct?
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #7
    Regular Coder
    Join Date
    Nov 2002
    Posts
    104
    Thanks
    13
    Thanked 0 Times in 0 Posts
    Oh Sorry I'm not sure what the foreign Key would be...

  • #8
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    You can't tie a photo in the photo_ads table to an ad in the ads table unless you have something to associate the two. You need a FK in the ads table that is the ad_id associated with the photo.

    Anyway, if you do put an FK in the photo_ads table that ties to the ad_id, the query would be something like:
    Code:
    SELECT a.*, b.*
    FROM ads a JOIN photo_ads b on b.ad_id = a.ad_id
    ORDER BY ad_id
    ... or something similar.
    Last edited by WolfShade; 07-17-2012 at 09:40 PM.
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".


  •  

    Posting Permissions

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