Jump to content

Checking for a value in a row before insterting


beennn

Recommended Posts

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 by beennn
Link to comment
Share on other sites

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 by beennn
Link to comment
Share on other sites

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...