我在添加问题时已经检查了这个建议,但我没有得到任何解决方案,所以我在这里提问。
我使用LEFT JOIN进行查询,但问题是我得到了结果,但格式和行是不同的。
请帮我解决这个问题。我试了半天但没有得到任何解决方案。
SELECT products_features.qid,features_values_int.value_int,a1.value_int FROM products_features
JOIN filters ON (products_features.feature_id = filters.filter_feature_id_min OR products_features.feature_id = filters.filter_feature_id_max)
left JOIN features_values_int ON features_values_int.feature_value_id = products_features.feature_value_id AND feature_id = 'F02501'
left JOIN features_values_int as a1 ON a1.feature_value_id = products_features.feature_value_id AND feature_id = 'F02502'
WHERE products_features.feature_id IN ('F02501','F02502')
我通过上述查询获得的结果如下:
我想要结果如下:
创建语句如下:
CREATE TABLE `filters` ( `id` int(11) NOT NULL AUTO_INCREMENT, `filter_id` varchar(16) NOT NULL, `filter_group_id` varchar(6) NOT NULL, `filter_sequence` int(11) NOT NULL, `filter_name_en` varchar(64) NOT NULL, `filter_name_fi` varchar(64) NOT NULL, `filter_type_id` varchar(6) NOT NULL, `filter_feature_id` varchar(16) NOT NULL, `filter_feature_id_min` varchar(16) NOT NULL, `filter_feature_id_max` varchar(16) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `filter_id` (`filter_id`), KEY `filter_group_id` (`filter_group_id`), KEY `filter_type_id` (`filter_type_id`)) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=latin1
CREATE TABLE `products_features` ( `id` int(11) NOT NULL AUTO_INCREMENT, `qid` varchar(32) NOT NULL, `feature_id` varchar(32) NOT NULL, `value_table` varchar(64) NOT NULL, `feature_value_id` varchar(10) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `feature_value_id` (`feature_value_id`), KEY `products_features` (`feature_id`)) ENGINE=InnoDB AUTO_INCREMENT=143862 DEFAULT CHARSET=latin1
CREATE TABLE `features_values_int` ( `id` int(11) NOT NULL AUTO_INCREMENT, `feature_value_id` varchar(10) NOT NULL, `value_int` int(32) NOT NULL, PRIMARY KEY (`id`), KEY `features_values_int` (`feature_value_id`)) ENGINE=InnoDB AUTO_INCREMENT=27327 DEFAULT CHARSET=latin1
示例数据如下:
答案 0 :(得分:2)
更正了您的查询,请尝试以下查询
SELECT T1.qid,
T1.value_int,
T2.value_int
FROM
(
SELECT products_features.qid,
features_values_int.value_int
FROM products_features
JOIN filters
ON ( products_features.feature_id = filters.filter_feature_id_min
OR products_features.feature_id = filters.filter_feature_id_max)
left JOIN features_values_int
ON features_values_int.feature_value_id = products_features.feature_value_id
AND feature_id = 'F02501'
WHERE value_int IS NOT NULL
) AS T1
INNER JOIN
(
SELECT products_features.qid,
a1.value_int
FROM products_features
JOIN filters
ON ( products_features.feature_id = filters.filter_feature_id_min
OR products_features.feature_id = filters.filter_feature_id_max)
left JOIN features_values_int as a1
ON a1.feature_value_id = products_features.feature_value_id AND feature_id = 'F02502'
WHERE value_int IS NOT NULL
) AS T2
ON T1.qid=T2.qid
<强>输出强>
qid value_int value_int
TUR813G12KAD4X 10 60
BAL110924 -35 100
BAL149236 -45 85
现场演示