Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.6k views
in Technique[技术] by (71.8m points)

php - MySQL don't insert row if it already exists

I'm running an insert query and before the statement runs, i want to check if the row already exists by every column. So I ran an if statement in php that uses a query to return the ID of a row with all the same values I'm about to insert. If it returns blank then it means i can insert the new row.

However some of the values in the row already there can be NULL and col = NULL wont work because it has to be col IS NULL. But I can't exclusively use IS because it could be an actual value. No idea what to do.

EDIT:

Let's say these 2 rows are already in the table

ID | NAME | AGE | COUNTRY
-------------------------
1  | John | 10  | Ireland
2  | Jane | 22  | NULL

and the query to check if the row i was about to insert exists is

SELECT ID FROM table WHERE name = ? AND age = ? AND country = ?

If the values were all equal to row 1, it would work but not row 2 because it would have to be country IS NULL. But there is no way of me knowing before hand if its gonna be null or not. It can also be like this for all columns, not just one


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...