我正在过滤买主(很多)给定属性(一个)
的主要条件buyers.bathroom_exact='1'
),请将其与酒店的浴室价值相匹配。 买家可以选择多个精确的浴室比赛。这是一个示例模式:
CREATE TABLE `buyers` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` text NOT NULL,
`bathroomreq` tinyint(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE `buyers_bathrooms` (
`buyer_id` int(10) unsigned NOT NULL DEFAULT '0',
`number` decimal(10,2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `buyers` ( name, bathroomreq ) VALUES ( 'John Doe', 1);
INSERT INTO `buyers` ( name, bathroomreq ) VALUES ( 'John Smith', 1);
INSERT INTO buyers_bathrooms ( buyer_id, number ) VALUES ( 1, 8 );
INSERT INTO buyers_bathrooms ( buyer_id, number ) VALUES ( 1, 9 );
INSERT INTO buyers_bathrooms ( buyer_id, number ) VALUES ( 1, 10 );
INSERT INTO buyers_bathrooms ( buyer_id, number ) VALUES ( 2, 5 );
我能想到这样做的唯一方法是使用一个具有IF
子句的列来查看bathroomreq是否为1
,然后匹配买家指定浴室的group_concat:
SELECT IF ( bathroomreq = '1', 8 IN (
SELECT GROUP_CONCAT(number) AS bathrooms
FROM buyers_bathrooms
WHERE buyers.id = buyers_bathrooms.buyer_id
) , 1 ) AS bathroom_match,
bathroomreq,
id, name
FROM buyers
所以这个属性基本上有8个浴室,这就是为什么8在那里硬编码。
这是一个mysql小提琴:
http://sqlfiddle.com/#!2/508cc/1
这是解决问题的唯一方法吗?它可靠吗?如果INNER JOIN
是bathroomreq
,我可以使用动态1
之类的替代方式吗?基本上,我可以避免在我的应用程序代码中基于bathroom_match
进行分支,并仅通过SQL来优雅地过滤它吗?
答案 0 :(得分:1)
我认为你可以用exists
子句做你想做的事:
select b.bathroomreq, b.id, b.name,
(b.bathroomreq <> 1 or
exists (select 1
from buyers_bathrooms bb
where bb.buyer_id = b.id and bb.number = 8
)
) as bathroom_match
from buyers b;