beennn Posted March 5, 2013 Share Posted March 5, 2013 (edited) How can I check a row for a number and then based on the result, perform an insert if the number isn't found? I need to ensure only one of a particular number is found in the row. Edited March 5, 2013 by beennn Link to comment Share on other sites More sharing options...
justsomeguy Posted March 5, 2013 Share Posted March 5, 2013 You could set unique indexes and use INSERT IGNORE, or do a select statement first to look for whatever you're looking for. Link to comment Share on other sites More sharing options...
beennn Posted March 5, 2013 Author Share Posted March 5, 2013 (edited) Thanks for the reply, could you show me or link me to some examples of both using indexes and select statement first to look for whatever you're looking for please? I've only used indexes within phpmyadmin for foreign keys. I'm not really familiar with how they are supposed to be used otherwise, do you check the index with the query you're running? or do they have an affect on the table? And the select method sounds ideal however I've been looking for an example of it but it sounds to simple for me to know what to search for if that makes any sense. Edited March 5, 2013 by beennn Link to comment Share on other sites More sharing options...
justsomeguy Posted March 5, 2013 Share Posted March 5, 2013 If you define columns as unique then MySQL will not insert a row with a duplicate value in that column. Using INSERT IGNORE says to ignore that error if it happens, so it will insert the row if everything is fine, or otherwise ignore it. If you want to check manually, then just select records with the your numbers in any of the columns that should be unique, and see if it returns any records. Link to comment Share on other sites More sharing options...
beennn Posted March 5, 2013 Author Share Posted March 5, 2013 Oooh, ok thanks. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now