views:

30

answers:

3
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| pid   | varchar(99) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
1 row in set (0.00 sec)


+-------+---------------+------+-----+---------+-------+
| Field | Type          | Null | Key | Default | Extra |
+-------+---------------+------+-----+---------+-------+
| pid   | varchar(2000) | YES  |     | NULL    |       |
| recid | varchar(2000) | YES  |     | NULL    |       |
+-------+---------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

This is my table. pid is just the id of the user. "recid" is a recommended song for that user.

I hope to have a list of pid's, and then recommended songs for each person. Of course, in my 2nd table, (pid, recid) would be unique key.

How do I do a one-to-one query for this ?

A: 
# retrieve all songs associated to a given user    
SELECT songs.*
FROM user
INNER JOIN songs ON (user.pid = songs.pid)
WHERE user.pid = ?
cballou
A: 

You may create FKs with query like

ALTER TABLE `foo`
ADD FOREIGN KEY (`bar_id`) REFERENCES `bar` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE

and see the mysql documentation. Also note that Mysql only supports foreign keys in InnoDB engine.

Mikulas Dite
A: 

You want to use a join (inner) to get the information out. Here is a refrence that I've used and have found helpful. Joins

Kris.Mitchell