tags:

views:

211

answers:

4
+2  Q: 

array in MySQL

+2  A: 

You might want a comma-separated value, but it's not recommended. Why not use another table for showing relationships?

Table2
----------------------
PARENT_ID | FRIEND_ID
----------------------
1         | 2
1         | 3
2         | 1
2         | 3
----------------------
Joset
A: 

Better ask google , this might help

Edit (About serializing arrays)

$conn = odbc_connect("webdb", "php", "chicken");
$stmt = odbc_prepare($conn,
      "UPDATE sessions SET data = ? WHERE id = ?");
$sqldata = array (serialize($session_data), $_SERVER['PHP_AUTH_USER']);
if (!odbc_execute($stmt, $sqldata)) {
    $stmt = odbc_prepare($conn,
     "INSERT INTO sessions (id, data) VALUES(?, ?)");
    if (!odbc_execute($stmt, $sqldata)) {
        /* Something went wrong.. */
    }
}
Ish Kumar
While an interesting solution, I would think a simple serialize() would be more performant in the long run and provide a simple interface for retrieving the data.
Erik
simple and good idea @Erik
Ish Kumar
How does one query a serialized array for rows matching Friends=3 as the OP asked for?
Bill Karwin
this will help in serializing array http://in.php.net/serialize and storing in database.
Ish Kumar
+13  A: 

Unless you have a really good reason for doing this, you should keep your data normalized and store the relationships in a different table. I think perhaps what you are looking for is this:

mysql> CREATE TABLE people (
    ->     id int not null auto_increment,
    ->     name varchar(250) not null,
    ->     primary key(id)
    -> );
Query OK, 0 rows affected (0.02 sec)

mysql>
mysql> CREATE TABLE friendships (
    ->     id int not null auto_increment,
    ->     user_id int not null,
    ->     friend_id int not null,
    ->     primary key(id)
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> INSERT INTO people (name) VALUES ('Bill'),('Charles'),('Clare');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> INSERT INTO friendships (user_id, friend_id) VALUES (1,3), (2,3);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql>
mysql> SELECT *
    -> FROM people p
    -> INNER JOIN friendships f
    -> ON f.user_id = p.id
    -> WHERE f.friend_id = 3;
+----+---------+----+---------+-----------+
| id | name    | id | user_id | friend_id |
+----+---------+----+---------+-----------+
|  1 | Bill    |  1 |       1 |         3 |
|  2 | Charles |  2 |       2 |         3 |
+----+---------+----+---------+-----------+
2 rows in set (0.00 sec)
Paolo Bergantino
Correct and the best way. This is the grammar of relational databases. Better use this solution - it is tested by time and experience. And, btw, I wonder, what will the moderation (sure, you'll need it) of serialized arrays look like??
Jet
Thanks, I used this solution.
Hintswen
+2  A: 

This looks like the perfect place for a relation table instead: Table 1:

ID, Name
1,  Bill
2,  Charles
3, Clare

Table 2 (the relation table)

ID, FriendID
1,  2
1,  3
2,  1
2,  3
3,  1

The second table keeps track of the friend relations by connecting ID's from Table1.

Fredrik Mörk