您好StackOverflow成员,
我正在尝试修复一些朋友的代码,但我并没有摆脱它。我们向它迈出了一步,但这不是确切的顺序。我先解释一下。
我们想要创建一个匹配系统。但不是基于用户,而是基于实际的房间基础。比赛按最大租金,最小平方米和所选城市进行。
现在我们已经创建了以下代码,它输出每个用户及其匹配的房间。
$members = $db->query("Select g.gFirstname, g.gLastname,g.gEmail, v.vMeter, v.vMaxrent, v.vWhen, v.vCity From users as g LEFT JOIN users_pref as v ON(g.gId=v.vGid)");
$count = $members->num_rows;
while($fetch = $members->fetch_assoc()){
echo '<br /><br />';
echo 'Rooms found for: <strong>'.stripslashes($fetch['gFirstname']).' '.stripslashes($fetch['gLastname']).'</strong><br /><br />';
$selectKamers = $db->query("Select * From rooms Where kWhen >= ".$fetch['vWhen']." AND kCity = ".$fetch['vCity']." AND kMeter >= '".$fetch['vMeter']."' AND kMaxrent <= ".$fetch['vMaxrent']."");
while($kamer = $selectKamers->fetch_assoc()){
echo '<h1>'.$kamer['kStreet'].' '.$kamer['kHomenumber'].'</h1>';
}
}
我说的这段代码首先显示所有用户,并在每个用户下方的列中显示用户应用的每个房间符合他的偏好。
现在的问题是。我们想要创建显示所有房间的功能,并在下面显示与该房间匹配的所有用户。
也许是因为缺乏洞察力,但有人可能会帮助我在路上如何实现这一目标?
我的数据库表如下:
--users
gId | gFistname | gLastname | gEmail
1 | Bob | Hope | Email
2 | John | Doe | Email
--users_pref
vGid | vMeter | vMaxrent | vWhen | vCity
1 | 10 | 400 | time() | cityId
2 | 20 | 500 | time() | cityId
--rooms
kWhen | kStreet | kHomenumber | kMeter | kMaxrent | kCity
time() | street | 100 | 15 | 300 | cityid
time() | street | 200 | 25 | 400 | cityid
如果我错过了什么,我试着尽可能完整地解释,请告诉我。
非常感谢提前!
凯文。
答案 0 :(得分:0)
考虑以下内容......
DROP TABLE IF EXISTS users;
CREATE TABLE users
(user_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,firstname VARCHAR(12) NOT NULL
,lastname VARCHAR(12) NOT NULL
,email VARCHAR(12) NOT NULL UNIQUE
);
INSERT INTO users VALUES
(1 ,'Bob','Hope','Email1'),
(2 ,'John','Doe','Email2');
DROP TABLE IF EXISTS user_preferences;
CREATE TABLE user_preferences
( user_id INT NOT NULL
, city_id INT NOT NULL
, date VARCHAR(30) NOT NULL
, meter INT NOT NULL
, maxrent INT NOT NULL
, PRIMARY KEY(user_id,city_id,date)
);
INSERT INTO user_preferences VALUES
(1,1,'time()',10,400),
(2,1,'time()',20,500);
DROP TABLE IF EXISTS rooms;
CREATE TABLE rooms
(city_id INT NOT NULL
,street VARCHAR(12) NOT NULL
,homenumber INT NOT NULL
,date VARCHAR(30) NOT NULL
,meter INT NOT NULL
,maxrent INT NOT NULL
,PRIMARY KEY(city_id,street,homenumber)
);
INSERT INTO rooms VALUES
(1,'street',100,'time()',15,300),
(1,'street',200,'time()',25,400);
要获得所有房间的列表以及任何兼容的用户,我们可以这样做......
SELECT r.*,u.*
FROM rooms r
LEFT
JOIN user_preferences ru
ON ru.city_id = r.city_id
AND ru.date = r.date
AND ru.meter <= r.meter
AND ru.maxrent >= r.maxrent
LEFT
JOIN users u
ON u.user_id = ru.user_id;
+---------+--------+------------+--------+-------+---------+---------+-----------+----------+--------+
| city_id | street | homenumber | date | meter | maxrent | user_id | firstname | lastname | email |
+---------+--------+------------+--------+-------+---------+---------+-----------+----------+--------+
| 1 | street | 100 | time() | 15 | 300 | 1 | Bob | Hope | Email1 |
| 1 | street | 200 | time() | 25 | 400 | 1 | Bob | Hope | Email1 |
| 1 | street | 200 | time() | 25 | 400 | 2 | John | Doe | Email2 |
+---------+--------+------------+--------+-------+---------+---------+-----------+----------+--------+