Hallo,
ich habe eine Tabelle mit zwei Spalten, die gemeinsam Unique sind. Es soll also keine zwei Zeilen geben, die gleich sind.
Gibt es dazu eine Möglichkeit in einer SQL-Anweisung zu überprüfen ob eine Zeile bereits existiert und sie anderen Falls einfügen?
Pascal
Mysql: Insert if
Re: Mysql: Insert if
RTFM:
If you specify the keyword IGNORE in an INSERT with many rows, any rows that duplicate an existing PRIMARY or UNIQUE key in the table are ignored and are not inserted. If you do not specify IGNORE, the insert is aborted if there is any row that duplicates an existing key value. You can determine with the C API function mysql_info() how many rows were inserted into the table.
Re: Mysql: Insert if
Danke, ist was ich meinte. :)
In zwischen Frage ich mich, ob ich das nicht lieber richtig abfangen will :
Ist nur das Problem, dass ich das in eine eigene Funktion auslagern muss, da
Einen parse-Error schmeißt.
[Update]
Ich glaube ich weiss jetzt, was ich will:
[/Update]
In zwischen Frage ich mich, ob ich das nicht lieber richtig abfangen will :
Code: Select all
if (mysql_errno != 1062) die(mysql_error());
else { .... }
Code: Select all
or {
if(mysql_errno()!= 1062) die(mysql_error());
else return false;
};
[Update]
Ich glaube ich weiss jetzt, was ich will:
Code: Select all
function profile_add_userrights($tid, $uid) {
$sql = "INSERT INTO `controll` SET UserID='" . $tid . "', TraegerID='" . $tid . "'";
$result = mysql_query($sql);
if(mysql_errno() != 1062) die(mysql_error());
else return false;
return true;
}