www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Server-Side Development > SQL

    SQL For all Structured Query Language, and general database questions.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1  
    Old 11-23-2009, 05:44 AM
    knightman's Avatar
    knightman knightman is offline
    Registered User
     
    Join Date: Dec 2007
    Posts: 58
    resolved [RESOLVED] insert / update / ignore

    Hello.


    ** i postet this on the php forum, but i think it should be here on SQL **


    let's say i want to add a row to a table called "todaypics" with this 3 columns:
    | picnum | imagename | todayhits |

    and i do this:
    PHP Code:
    mysql_query("INSERT INTO todaypics (picnum, imagename, todayhits) VALUES('72', 'somepicturename.jpg', '35') ")
    or die(
    mysql_error());
    the data is inserted
    | 72 | somepicturename.jpg | 35 |
    witn no problem.

    but if the row was already on the
    tablewith this data:...
    | 72 | somepicturename.jpg | 34 |

    ...how sohuld be the query to avoid the duplicate error and update the column "todayhits" value to 35??

    ------------------------------------------------------------------


    other situation i have:

    on the other table named "pics", i have hundreds of rows.
    the columns are:
    | picnum | imagename | hits |

    if i want to display the 1st 50 images sort by hits i do this:

    PHP Code:


    $result
    = mysql_query("SELECT * FROM pics ORDER BY hits desc LIMIT 50")
    or die(
    mysql_error());  
    echo
    "<table border='1'>";
    echo
    "<tr> <th> PicNum </th> <th> Picture </th> <th> Views </th></tr>";
    // keeps getting the next row until there are no more to get
    while($row = mysql_fetch_array( $result )) {
        
    // Print out the contents of each row into a table
        
    echo "<tr><td>";
        echo
    $row['pid'];
        echo
    "</td><td>";
        echo
    "<a href=\"/image-path/".$row['imagename']."\" target=\"_blank\"><img src=\"/thumbnail-path/".$row['imagename']."\" /><a/>";
        echo
    "</td><td>";
        echo
    $row['hits'];

        echo
    "</td></tr>";
    }
    echo
    "</table>";

    if 50 images are to be displayed, but only 25 has some hits, the rest of the images are ordered by picnum..

    is there a way to ignore the rows when the "hits" value is zero?
    Reply With Quote
      #2  
    Old 11-23-2009, 08:11 AM
    yamaharuss's Avatar
    yamaharuss yamaharuss is offline
    V8 Rider
     
    Join Date: Jan 2008
    Location: Florida
    Posts: 684
    Code:
    If NOT EXISTS (Select imagename from todaypics where imagename = 'somepicturename.jpg')  INSERT INTO todaypics (picnum, imagename, todayhits) VALUES('72', 'somepicturename.jpg', '35')
    Code:
    SELECT TOP 50 * FROM pics ORDER BY hits desc
    __________________
    Internet Design Concepts
    Web Development and Hosting
    InternetDesignConcepts.com
    Reply With Quote
      #3  
    Old 11-23-2009, 11:26 AM
    knightman's Avatar
    knightman knightman is offline
    Registered User
     
    Join Date: Dec 2007
    Posts: 58
    Thanks yamaharuss...
    i'll try it tonight and will let you know.
    Reply With Quote
      #4  
    Old 11-23-2009, 11:48 AM
    ssystems ssystems is offline
    Registered User
     
    Join Date: Oct 2009
    Posts: 278
    Quote:
    Originally Posted by yamaharuss View Post
    Code:
    SELECT TOP 50 * FROM pics ORDER BY hits desc
    Keep in mind you're using MySQL and to ignore the pics w/ 0 hits

    Code:
    SELECT * FROM pics WHERE hits > 0 ORDER BY hits desc LIMIT 50
    __________________
    Good Luck

    Santos Systems
    Reply With Quote
      #5  
    Old 11-23-2009, 10:56 PM
    knightman's Avatar
    knightman knightman is offline
    Registered User
     
    Join Date: Dec 2007
    Posts: 58
    resolved

    yamaharuss:
    i tried this:
    PHP Code:
    If NOT EXISTS (Select imagename from todaypics where imagename = 'somepicturename.jpg')  INSERT INTO todaypics (picnum, imagename, todayhits) VALUES('72', 'somepicturename.jpg', '35')
    but didn't work. so after some googling, found this:
    PHP Code:
    mysql_query("INSERT INTO todaypics (picnum, imagename, todayhits)
    Select Distinct 72, somepicturename.jpg, 35
    (Select * From todaypics Where picnum == 72"
    )
    or die(
    mysql_error());
    this one works great!
    -----------------------------------------------

    ssystems:
    PHP Code:
    SELECT * FROM pics WHERE hits > 0 ORDER BY hits desc LIMIT 50
    was displaying the files with 0 hits, but i found this:
    PHP Code:
    SELECT * FROM pics WHERE trim(hits) != '0' ORDER BY hits desc LIMIT 50
    this work ok!

    thank you both...

    KnightMan
    Reply With Quote
      #6  
    Old 11-23-2009, 11:07 PM
    ssystems ssystems is offline
    Registered User
     
    Join Date: Oct 2009
    Posts: 278
    I see your hits column was a varchar
    __________________
    Good Luck

    Santos Systems
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 05:37 AM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.