+-------+-------------+------+-----+---------+-------+
| 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 ?
# retrieve all songs associated to a given user
SELECT songs.*
FROM user
INNER JOIN songs ON (user.pid = songs.pid)
WHERE user.pid = ?
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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.