我在这个有点失落,我有一个问题,根据它的关系表的值在表上显示行。我不知道如何去做这件事。我的预期结果如下:MySQL:基于关系表过滤行
预期结果
-- Expected Result #1: (user_id = 1 only, applied)
------ -------- ------- --------
id position user_id meta_key
------ -------- ------- --------
1 Waiter 1 applied
2 Busboy (NULL) (NULL)
3 Driver (NULL) (NULL)
4 Chef 1 applied
------ -------- ------- --------
-- Expected Result #2: Same as above except filtered by category IN(98)
------ -------- ------- --------
id position user_id meta_key
------ -------- ------- --------
1 Waiter 1 applied (uses category: 98, 99)
2 Busboy (NULL) (NULL) (uses category: 98)
3 Driver (NULL) (NULL) (uses category: 98)
------ -------- ------- --------
我的表
Table 1: Position
------ -------- ---------
id position user_id
------ -------- ---------
1 Waiter 1
2 Busboy 1
3 Driver 2
4 Chef 3
------ -------- ---------
Table 2: Meta
------ ------- ----------- -------- ------------
id user_id position_id meta_key meta_value
------ ------- ----------- -------- ------------
1 1 1 category 98
2 1 1 category 99
3 1 2 category 98
4 2 3 category 98
5 3 4 category 100
7 1 1 applied 1
8 2 1 applied 1
9 3 1 applied 1
10 1 4 applied 1
------ ------- ----------- -------- ------------
在我预期的结果,正在显示时被过滤它除了所有位置一个或多个类别。用户基本上“申请”职位,并显示他们的user_id,如果他们适用于它。但是如果条目来自他们不适用的位置,该位置仍然显示,但user_id
字段NULL
。
MySQL有可能做类似这样的事情吗?我最初的查询能够完成第一个预期的结果,但不是第二个按类别过滤的结果。
我的查询(USER_ID = 1)
SELECT pos.id, pos.position, meta.user_id, meta.meta_key
FROM `position` pos
LEFT JOIN meta ON pos.id = meta.position_id
AND meta.meta_key = 'applied'
AND meta.user_id = 1
SQL文件
SET @[email protected]@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @[email protected]@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @[email protected]@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
-- -----------------------------------------------------
-- Table `position`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `position` ;
CREATE TABLE IF NOT EXISTS `position` (
`id` INT NOT NULL,
`position` VARCHAR(191) NULL,
`user_id` INT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `meta`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `meta` ;
CREATE TABLE IF NOT EXISTS `meta` (
`id` INT NOT NULL,
`user_id` INT NULL,
`position_id` INT NULL,
`meta_key` VARCHAR(191) NULL,
`meta_value` VARCHAR(191) NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
SET [email protected]_SQL_MODE;
SET [email protected]_FOREIGN_KEY_CHECKS;
SET [email protected]_UNIQUE_CHECKS;
-- -----------------------------------------------------
-- Data for table `position`
-- -----------------------------------------------------
START TRANSACTION;
INSERT INTO `position` (`id`, `position`, `user_id`) VALUES (1, 'Waiter', 1);
INSERT INTO `position` (`id`, `position`, `user_id`) VALUES (2, 'Busboy', 1);
INSERT INTO `position` (`id`, `position`, `user_id`) VALUES (3, 'Driver', 2);
INSERT INTO `position` (`id`, `position`, `user_id`) VALUES (4, 'Chef', 3);
COMMIT;
-- -----------------------------------------------------
-- Data for table `meta`
-- -----------------------------------------------------
START TRANSACTION;
INSERT INTO `meta` (`id`, `user_id`, `position_id`, `meta_key`, `meta_value`) VALUES (1, 1, 1, 'category', '98');
INSERT INTO `meta` (`id`, `user_id`, `position_id`, `meta_key`, `meta_value`) VALUES (2, 1, 1, 'category', '99');
INSERT INTO `meta` (`id`, `user_id`, `position_id`, `meta_key`, `meta_value`) VALUES (3, 1, 2, 'category', '98');
INSERT INTO `meta` (`id`, `user_id`, `position_id`, `meta_key`, `meta_value`) VALUES (4, 2, 3, 'category', '98');
INSERT INTO `meta` (`id`, `user_id`, `position_id`, `meta_key`, `meta_value`) VALUES (5, 3, 4, 'category', '100');
INSERT INTO `meta` (`id`, `user_id`, `position_id`, `meta_key`, `meta_value`) VALUES (7, 1, 1, 'applied', '1');
INSERT INTO `meta` (`id`, `user_id`, `position_id`, `meta_key`, `meta_value`) VALUES (8, 2, 1, 'applied', '1');
INSERT INTO `meta` (`id`, `user_id`, `position_id`, `meta_key`, `meta_value`) VALUES (9, 3, 1, 'applied', '1');
INSERT INTO `meta` (`id`, `user_id`, `position_id`, `meta_key`, `meta_value`) VALUES (10, 1, 4, 'applied', '1');
COMMIT;
你能给我们的样本数据为ddl吗? – Philipp
ddl是什么意思? – enchance
他的意思是这样的:http://sqlfiddle.com/#!9/a546f –