Jump to content

[SOLVED] double unique


rarebit

Recommended Posts

Hey,

 

Let's say I have a table (not that it's like this at all, just so you get the id):

$s = "CREATE TABLE test_unique (user varchar(32) unique, buddy varchar(32) unique )";
$ret = mysql_query($s, $conn);

$s = "INSERT INTO test_unique VALUES ('me', 'you'), ('me', 'them'), ('you', 'me'), ('me', 'them') ";
$res = mysql_query($s, $conn) or die(mysql_error());

(hmm, even the above won't insert all of 'em!)

 

What I wanna do is insert them and these:

$s = "INSERT INTO test_unique VALUES ('them', 'you'), ('them', 'me') ";

 

Do you see what I mean?

 

I want to block duplicates in a single mysql call, based on each user...

 

 

Link to comment
https://forums.phpfreaks.com/topic/108101-solved-double-unique/
Share on other sites

mmm, reading it doesn't really make much sense...

 

As it currently stands there are no unique fields in my table. I have to search for each name/buddy pair and if not there, insert it. However I want to do this in a single call and was trying to figure it using unique, but really it won't do it...

 

???

Link to comment
https://forums.phpfreaks.com/topic/108101-solved-double-unique/#findComment-554267
Share on other sites

mysql> CREATE TABLE `test_unique` (
    ->   `user` varchar(32) default NULL,
    ->   `buddy` varchar(32) default NULL,
    ->   UNIQUE KEY `user` (`user`,`buddy`)
    -> );
Query OK, 0 rows affected (0.17 sec)

mysql> INSERT INTO test_unique VALUES
    -> ('me', 'you'),
    -> ('me', 'them'),
    -> ('you', 'me'),
    -> ('them', 'you'),
    -> ('them', 'me');
Query OK, 5 rows affected (0.06 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> SELECT * FROM test_unique;
+------+-------+
| user | buddy |
+------+-------+
| me   | them  |
| me   | you   |
| them | me    |
| them | you   |
| you  | me    |
+------+-------+
5 rows in set (0.00 sec)

 

This fails as the combination already exists

 

INSERT INTO test_unique VALUES ('me', 'them')

Link to comment
https://forums.phpfreaks.com/topic/108101-solved-double-unique/#findComment-554425
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.