我有三张桌子: 表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());
答案 0 :(得分:7)
你在寻找这样的东西吗?:
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