2

我有三个表:表 1 合同

-------------------
id  |  contract_name
--------------------
1      test name
2      test name 2
2      test name 3
4      test name 4

表 2 房间

-------------------
id  |  room_name
--------------------
1      test name
2      test name 2
2      test name 3
4      test name 4

表 3 促销

----------------------------------
 id  |  contracts_id  |   rooms_id
----------------------------------
 1          1,3            1,3,4
 1          2              1,2,3

不,我正在尝试进行内部连接,以根据保存在数据库中的数组中的 id 获取合同和房间的名称。我知道这根本不理想,但我无法更改数据库设置。所以这就是我想对我的查询做的事情,但显然这是不可能的。有谁知道我如何做到这一点?

mysql_query("SELECT pb.*, c.contract_name, r.room_name FROM promo_blackouts AS pb
INNER JOIN contracts as c ON c.contract_id IS IN pb.contracts_id
INNER JOIN rooms as r ON r.room_id IS IN pb.rooms_id 
WHERE pb.promo_id = '$promo_id'") or die(mysql_error());
4

1 回答 1

8

你在寻找这样的东西吗?:

SELECT DISTINCT
    contract_name,
    room_name
FROM
    promos
INNER JOIN
    contracts ON FIND_IN_SET(contracts.id, contract_id) != 0
INNER JOIN
    rooms ON FIND_IN_SET(rooms.id, room_id) != 0
WHERE
    promos.id = 1
于 2012-11-08T01:18:42.003 回答