how can i make a query to display from two table

Discussion in 'PHP' started by baris22, Jan 20, 2008.

  1. #1
    Hello,

    I have got two tables. One of them is about the videos and the other one is about the ratings for that videos. I want to display the videos by the ratings. How can i make a query to do this?

    This is the query to display videos by most visited

    
    $sql = "select uniq_id, artist, video_title, yt_views, site_views, added FROM pm_videos WHERE category LIKE '%$catid%' ORDER BY yt_views DESC LIMIT 50" ;
    
    Code (markup):
    the table for the ratings is called: pm_ratings and connected to pm_videos by id.

    Thank you for your help.
     
    baris22, Jan 20, 2008 IP
  2. VeRSioN

    VeRSioN Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    i think there is a default command " join " .. but i don't know how to use :( but you can search in php.net..
     
    VeRSioN, Jan 20, 2008 IP
  3. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Try this:

    $sql = "select category.uniq_id, category.artist, category.video_title, category.yt_views, category.site_views, category.added FROM pm_videos, pm_ratings WHERE pm_ratings.id=pm_videos.id AND category LIKE '%$catid%' ORDER BY pm_ratings.rating DESC LIMIT 50" ;
    PHP:
    Brew
     
    Brewster, Jan 20, 2008 IP
  4. jayasimha

    jayasimha Peon

    Messages:
    48
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Please let me know the columns in each of the two tables and the columns you want displayed in the output of the query

    cheers,
    jay
     
    jayasimha, Jan 22, 2008 IP
  5. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #5
    pm_ratings has got id, total_votes, total_value.
    pm_videos has got id, uniq_id.

    They are connected to each other by id and uniq_id

    pm_videos uniq_id is pm_ratings id.

    I want to show the videos by total_value

    thanks



     
    baris22, Jan 22, 2008 IP
  6. jayasimha

    jayasimha Peon

    Messages:
    48
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Try:

    [COLOR="Blue"]$sql = [COLOR="Red"]"SELECT a.id, uniq_id, total_value FROM pm_videos a, pm_ratings b WHERE a.uniq_id = b.id ORDER BY total_value DESC"[COLOR="Blue"] ;[/COLOR]
    [/COLOR][/COLOR]
    Code (markup):
     
    jayasimha, Jan 22, 2008 IP
    baris22 likes this.
  7. admins

    admins Peon

    Messages:
    74
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I am giving simple example and also generalize that will able to solve your problem.
    Say table TA: id, val1, val2; table TB: id, val3,val4.
    Then the query is:
    Select a.*, b.* from TA a inner join TB b where a.id=b.id

    Thus you can use joining inner, left outer, right outer, full outer or cross joining.
    But inner join is most necessary, also 2nd and 3rd.
     
    admins, Jan 23, 2008 IP
  8. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #8
    Thank you I managed to get it working..

     
    baris22, Jan 23, 2008 IP
  9. jayasimha

    jayasimha Peon

    Messages:
    48
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Baris22, any reputation point for my post above, if it really helped you ? I am very new here and any reputation point earned will be of great help.

    Thanks.

    cheers,
    jay
     
    jayasimha, Jan 24, 2008 IP
  10. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #10
    Rep. added, thanks



     
    baris22, Jan 24, 2008 IP