当未包含语句末尾的WHERE子句时,子查询会完美地限制结果。我知道子查询LIMIT首先发生,然后在该结果集上触发WHERE子句,这是子查询的限制/限制。
我需要的是比我更有经验的人帮助兄弟用LIMIT检索记录,并且能够限制WHERE子句的结果集。如果这个解释不够好,请告诉我。
我还搜索了互联网,寻找答案几个小时没有运气。感谢您的时间。
编辑:在SQLfiddle上添加了一个粗略的例子:http://sqlfiddle.com/#!9/2de563/4
SELECT *
FROM (SELECT * FROM parent_products LIMIT 10) pp
INNER JOIN products_variants pv ON pv.parent_id=pp.parent_id
INNER JOIN products p ON p.id=pv.product_id
INNER JOIN product_types pt ON pt.product_type_id=p.product_type
LEFT JOIN team_list t ON pp.team_id=t.team_id
LEFT JOIN photos ph ON ph.product_id=p.id
LEFT JOIN product_attributes pa ON pa.product_id=pv.product_id
LEFT JOIN attributes a ON a.id=pa.attribute_id
LEFT JOIN product_attribute_options po ON po.product_attribute_option_id=a.parent_id
WHERE t.team_id=100 AND p.active='y';
答案 0 :(得分:0)
Below query will give you the expected output
SELECT * FROM (SELECT users.id,users.name FROM users
LEFT JOIN map ON users.id=map.user_id
LEFT JOIN locations ON locations.location_id=map.location_id
INNER JOIN type ON locations.type=type.id
WHERE type.id=1 limit 3) users
LEFT JOIN map ON users.id=map.user_id
LEFT JOIN locations ON locations.location_id=map.location_id
INNER JOIN type ON locations.type=type.id
where type.id=1;