MySQL查询引用多对多表

标签 mysql database

假设下表:

area
-id
-title

properties
-id
-title
-area_id

categories
-id
-title

properties_categories
-property_id
-category_id

以下查询非常接近正确答案,但并不正确。您能告诉我正确的查询是什么吗?

select a.id, a.title, count(p.id) 
FROM area AS a,properties AS p, properties_categories AS pc 
WHERE a.id = p.area_id 
  AND pc.category_id IN (1,2,3) 
  AND pc.property_id = p.id 
GROUP BY a.id;

或者

SELECT A.id, A.title, COUNT(B.id)
FROM area A LEFT JOIN  properties B 
ON A.id=B.area_id 
JOIN properties_categories C 
ON C.property_id=B.id
WHERE C.category_id IN (1,2,3)
GROUP BY A.id, A.title;

如果我编写一个查询,其中以下内容仅从区域和属性表中提取,您将得到正确的结果。

SELECT A.id, A.title, COUNT(B.id) FROM area A LEFT JOIN properties B ON A.id=B.area_id GROUP BY A.id, A.title

这是要试验的数据转储:

CREATE TABLE `area` (
  `id` int(10) UNSIGNED NOT NULL,
  `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


INSERT INTO `area` (`id`, `title`) VALUES
(2, 'Test1'),
(3, 'Test2'),
(4, 'Test3'),
(5, 'Test4'),
(6, 'Test5');


CREATE TABLE `categories` (
  `id` int(10) UNSIGNED NOT NULL,
  `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


INSERT INTO `categories` (`id`, `title`) VALUES
(1, 'Category A'),
(2, 'Category B'),
(3, 'Category C'),
(4, 'Category D'),
(5, 'Category E'),
(6, 'Category F');


CREATE TABLE `properties` (
  `id` int(11) NOT NULL,
  `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `area_id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


INSERT INTO `properties` (`id`, `name`, `area_id`) VALUES
(2, 'Property A', 2),
(3, 'Property B', 2),
(4, 'Property C', 2),
(5, 'Property D', 2),
(7, 'Property E', 2),
(8, 'Property F', 2),
(10, 'Property G', 2),
(11, 'Property H', 2),
(12, 'Property I', 2),
(13, 'Property J', 2),
(14, 'Property K', 2),
(19, 'Property L', 2),
(20, 'Property M', 4),
(21, 'Property O', 2),
(22, 'Property P', 2),
(23, 'Property Q', 2),
(24, 'Property  R', 2),
(27, 'Property S', NULL),
(29, 'Property T', 2),
(30, 'Property U', 2),
(32, 'Property V', 2),
(33, 'Property W', 2),
(34, 'Property X', 5),
(35, 'Property Y', 5),
(36, 'Property Z', 5),
(37, 'Property A1', 5),
(38, 'Property A2', 3),
(39, 'Property A3', 6);

CREATE TABLE `properties_categories` (
  `category_id` int(11) NOT NULL,
  `property_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

INSERT INTO `properties_categories` (`category_id`, `property_id`) VALUES
(2, 2),
(6, 2),
(2, 3),
(6, 3),
(2, 4),
(6, 4),
(2, 5),
(6, 5),
(2, 7),
(6, 7),
(2, 8),
(6, 8),
(2, 10),
(6, 10),
(2, 11),
(6, 11),
(2, 12),
(6, 12),
(2, 13),
(6, 13),
(2, 14),
(6, 14),
(5, 7),
(5, 3),
(5, 14),
(5, 4),
(5, 12),
(5, 11),
(5, 13),
(5, 5),
(5, 10),
(5, 8),
(2, 20),
(3, 20),
(2, 19),
(6, 19),
(2, 21),
(2, 22),
(2, 23),
(5, 23),
(6, 23),
(1, 22),
(2, 24),
(6, 24),
(2, 29),
(2, 30),
(5, 30),
(2, 33),
(5, 33),
(6, 33),
(2, 32),
(5, 32),
(6, 32),
(1, 34),
(2, 34),
(3, 34),
(4, 34),
(2, 35),
(5, 35),
(2, 36),
(4, 36),
(5, 36),
(2, 37),
(4, 37),
(5, 37),
(2, 38),
(2, 39),
(4, 39),
(5, 39),
(1, 39),
(5, 24),
(4, 38);

最佳答案

试试这个:

SELECT A.id, A.title, COUNT(B.id)
FROM area A LEFT JOIN  properties B 
ON A.id=B.area_id 
JOIN properties_categories C 
ON C.property_id=B.id
WHERE C.category_id IN (1,2,3)
GROUP BY A.id, A.title;

参见MySQL Join Made Easy了解有关在 mysql 中使用联接的见解。

关于MySQL查询引用多对多表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49537662/

相关文章:

mysql - 使用 REF_CURSOR 转换 Oracle 存储过程并将全局变量打包到 Postgresql 或 MySQL

python - South 在尝试迁移时引发 ValueError

MySQL:选择按一列分组的所有条目,该列在另一列中具有公共(public)值

mysql - 显示MySQL中第二个表中不存在的记录

arrays - MongoDB:计算文档中数组中存在多少具有给定值的项目?

php - 除了随机 ID 检索之外,在表中存储数据的最简单数据库解决方案是什么?

php - 在 PHP 中创建链接的点击计数

mysql - 搜索并返回与组中搜索匹配的行

javascript - 根据Rails 3中另一个collection_select中的选定项目自动填充text_fields

mysql - 获取 MySQL 独特的组合键