我有两张桌子colorcode
ID colorid colorname
------------------------
1 1 yellow
2 2 black
3 3 red
4 4 white
users
ID userid colorid
------------------------
1 1 1,2
2 2 3,4
3 3 1,3,4
4 4 1
如何检索和查询个人
colorcode
$aa = $db->query("SELECT * FROM colorcode");
$colors = array();
while ($colordata = mysql_fetch_assoc($aa)) {
$colors[] = $colordata["colorid"];
}
假设我想要查询哪些用户有
users
颜色&我应该使用什么语句colorid
SELECT .. FROM users
WHERE colorid ....
最佳答案
SELECT * FROM users
WHERE colorid LIKE "%1%"
但我真正要做的是制作一个从用户到颜色的链接表:
用户颜色:
ID userid colorid
------------------------
1 1 1
2 1 2
3 2 3
4 2 4
...
然后你可以:
SELECT * FROM users u, usersToColors utc
WHERE u.userid = utc.userid
AND utc.colorid = 1;
甚至:
SELECT * FROM users u, usersToColors utc, colors c
WHERE u.userid = utc.userid
AND utc.colorid = c.colorid
AND c.colorname = "yellow";