0

嗨 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

如果我遗漏任何内容,我会尽可能完整地解释我的解释,请告诉我。

提前谢谢了!

凯文。

4

1 回答 1

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 |
 +---------+--------+------------+--------+-------+---------+---------+-----------+----------+--------+
于 2013-10-18T00:27:18.583 回答