Struggling with a query

Discussion in 'MySQL' started by scottlpool2003, Sep 12, 2012.

  1. #1
    I'm struggling with queries within queries...

    I need to pull out the top 3 publications, but am struggling because the publications are split into 2 tables. They're split into 2 pages because table1 (publication) is the generic holding page of a publication with the info, whereas table 2 (publication_issue) is the individual issues of the 1 publication.

    2 Tables

    [1] Publication
    id
    title
    text
    cat
    subcat
    user_id
    use_ad
    viewcount

    [2] Publication Issue
    id
    user_id
    publication_id
    issue_number
    img
    pdf
    upload_date
    cat

    I've tried a few ways, the best I got it looking was pulling 4 out (when I limited it to 3) but the first 2 had duplicate viewcount fields.

    <?php
    //Get most viewed publications
    	$resultb = mysql_query("SELECT * FROM publication ORDER BY viewcount DESC");
    while($rowb = mysql_fetch_array($resultb))
    {
    //Check if no results
    if(mysql_num_rows($resultb)==0){
    
    }
    
    //There are results, get data
    else 
    {
    
    $id = $rowb[id];
    $cat = $rowb[cat];
    $subcat = $rowb[subcat];
    $viewcount = $rowb[viewcount];
    $title = $rowb[title];
    
    //Get publication info
    	$resulth = mysql_query("SELECT * FROM publication_issue WHERE publication_id = '$id' ORDER BY id DESC LIMIT 3");
    while($rowh = mysql_fetch_array($resulth))
    {
    $img = $rowh[img];
    $title = $rowh[title];
    
    	$img = $rowh[img];
    	echo "<div class=\"featcontainer\">";
    	  echo "<div class=\"left\">$viewcount</div>";
    	    echo "<div class=\"mid\"><a href=\"#\"><img src=\"/images/uploads/publications/image/big/$img\" height=\"90\" width=\"76\"></a></div>";
    		  echo "<div class=\"right1\"><a href=\"\">$title</a></div>";
    	echo"</div>";
    }}}
    
    ?>
    PHP:
     
    scottlpool2003, Sep 12, 2012 IP
  2. plussy

    plussy Peon

    Messages:
    152
    Likes Received:
    5
    Best Answers:
    9
    Trophy Points:
    0
    #2
    It looks to me that your code shows the the last 3 issues from all publications ordered by viewcount

    Do you want to display the top 3 Publications or the top 3 Issues??
     
    plussy, Sep 28, 2012 IP
  3. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #3
    Thanks for the reply

    I want to display the top 3 issues, but some of the attributes such as title are stored in the publication table.

    The reason they are split in 2 is because it's a 1 to many. 1 issue has default text, advert and subcategory whereas the issue does not contain text, advert, subcategory.

    PUBLICATION
    |
    ISSUE ISSUE ISSUE
     
    scottlpool2003, Oct 1, 2012 IP
  4. plussy

    plussy Peon

    Messages:
    152
    Likes Received:
    5
    Best Answers:
    9
    Trophy Points:
    0
    #4
    do you want to display the top 3 issues for all publications or just for one publication?
     
    plussy, Oct 1, 2012 IP
  5. plussy

    plussy Peon

    Messages:
    152
    Likes Received:
    5
    Best Answers:
    9
    Trophy Points:
    0
    #5
    well either way you need to move viewcount to the issue table and then do something like this

    
    SELECT * FROM publication_issue INNER JOIN publication ON publication_issue.publication_id=publication.id WHERE publcation.id=[PUBLICATION_ID_HERE] ORDER BY publication_issue.viewcount DESC LIMIT 3;
    
    Code (markup):
    this should give you the top 3 issues for a specific publication issue

    not sure if it is 100% correct though. give it a go in phpmyadmin to see what it returns.
     
    plussy, Oct 1, 2012 IP
  6. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #6
    Thanks for helping... I'm clueless with INNER JOIN but gave it a go anyway but it doesn't output anything:

    $result_pub=mysql_query("
    	SELECT * FROM publication
    	ORDER BY viewcount DESC
    
    ");
    $result=mysql_query("SELECT * FROM publication_issue INNER JOIN publication ON publication_issue.publication_id=publication.id WHERE publication.id=".$row_pub[id]." ORDER BY publication_issue.viewcount DESC LIMIT 3");
    	while ($row = mysql_fetch_array($result)) {
    				echo'	<img
    				src="/uploader/',$row['img'],'"
    				height="90" width="76"
    				alt="" align="left" 
    			/>
    				';
    }
    PHP:
     
    scottlpool2003, Oct 1, 2012 IP
  7. plussy

    plussy Peon

    Messages:
    152
    Likes Received:
    5
    Best Answers:
    9
    Trophy Points:
    0
    #7
    your code is wrong and my query is wrong

    try this

    please make sure that publication_issue has also viewcount
    
    
    $result_pub=mysql_query("
        SELECT * FROM publication
        ORDER BY viewcount DESC
    
    ");
    while ($res = mysql_fetch_assoc($result_pub)) {
    $result=mysql_query("SELECT * FROM publication_issue INNER JOIN publication ON publication_issue.publication_id=publication.id WHERE publication.id=".$res[id]." ORDER BY publication_issue.viewcount DESC LIMIT 3");
        while ($row = mysql_fetch_assoc($result)) {
                    echo'   <img
                    src="/uploader/',$row['img'],'"
                    height="90" width="76"
                    alt="" align="left" 
                />
                    ';
    }
    }
    
    
    PHP:
     
    plussy, Oct 1, 2012 IP
  8. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #8
    Thanks, a little closer now but still a strange issue.

    It's pulling out 2 items 3 times so I changed img to id to double check and I get:

    Is the first query messing the 2nd query up?
     
    scottlpool2003, Oct 1, 2012 IP
  9. plussy

    plussy Peon

    Messages:
    152
    Likes Received:
    5
    Best Answers:
    9
    Trophy Points:
    0
    #9
    try this one

    
    SELECT pi.id as issue_id, * FROM publication_issue pi INNER JOIN publication p ON pi.publication_id=p.id WHERE p.id=".$res[id]." ORDER BY pi.viewcount DESC LIMIT 3
    
    Code (markup):
    then update your code so that where you echo the issue id you echo issue_id instead
     
    plussy, Oct 1, 2012 IP
  10. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #10
    No output :-(
     
    scottlpool2003, Oct 1, 2012 IP