我收到了以下问题:
SELECT SQL_CALC_FOUND_ROWS u.unit_id, u.unit_serial_full, u.unit_status, u.contract_id, u.warehouse_id,
m.model_name,
c.contract_name,
t.transfer_id,
w.warehouse_name_full
FROM units AS u
LEFT JOIN models AS m ON m.model_id = u.model_id
LEFT JOIN contracts AS c ON c.contract_id = u.contract_id
LEFT JOIN transfers AS t ON t.transfer_id = u.transfer_id
LEFT JOIN warehouses AS w ON w.warehouse_id = u.warehouse_id
LEFT JOIN units_attributes AS ua ON ua.unit_id = u.unit_id
WHERE 1
AND u.company_id = :company_id
AND (
(
u.model_id = "4"
AND (
ua.attribute_id = :attribute_id_0 AND attribute_default = :attribute_value_0
)
AND (
ua.attribute_id = :attribute_id_1 AND attribute_default = :attribute_value_1
)
)
)
如果我使用2个或更多属性进行搜索,则不会产生任何结果,但如果我只有1个属性,则查询会按预期工作。有什么想法吗?
搜索必须严格。不是用通配符。
感谢。
答案 0 :(得分:0)
嗯,ua.attribute_id不能同时是两个值
(
ua.attribute_id = :attribute_id_0 AND attribute_default = :attribute_value_0
)
AND (
ua.attribute_id = :attribute_id_1 AND attribute_default = :attribute_value_1
)
答案 1 :(得分:0)
SELECT SQL_CALC_FOUND_ROWS u.unit_id, u.unit_serial_full, u.unit_status, u.contract_id, u.warehouse_id, m.model_name, c.contract_name, t.transfer_id, w.warehouse_name_full
FROM units AS u
LEFT JOIN models AS m ON m.model_id = u.model_id
LEFT JOIN contracts AS c ON c.contract_id = u.contract_id
LEFT JOIN transfers AS t ON t.transfer_id = u.transfer_id
LEFT JOIN warehouses AS w ON w.warehouse_id = u.warehouse_id
LEFT JOIN units_attributes AS ua ON ua.unit_id = u.unit_id
WHERE 1 = 1
AND u.company_id = :company_id
AND u.model_id = "4"
AND ua.attribute_id IN (:attribute_id_0, :attribute_id_1)
AND attribute_default IN (:attribute_value_0, :attribute_value_1)
答案 2 :(得分:0)
列不能同时具有两个不同的值。因此,ua.attribute_id = :foo AND ua.attribute_id = :bar
等过滤器会生成Impossible WHERE
子句。
最佳解决方案通常是分组您的结果,并仅为包含符合条件的记录的结果组过滤结果组,即使用HAVING
子句:
GROUP BY col1, col2, col3
HAVING SUM(ua.attribute_id = :foo) AND SUM(ua.attribute_id = :bar)