如何对具有多个相互依赖的左连接的查询强加左连接限制?请参阅以下有关LIMIT的评论:
With ActiveDocument.Shapes("Text Box 7").TextFrame.TextRange
.Style = "Style1"
.Text = str1 & " "
.Collapse wdCollapseEnd
.Style = "Style2"
.Text = str2 & ", "
.Collapse wdCollapseEnd
.Style = "Style3"
.Text = str3
End With
样本数据集......
SELECT
a.*,GROUP_CONCAT(c.body SEPARATOR ' ') AS bodies
FROM a
LEFT JOIN b ON b.id_a=a.id
LEFT JOIN c ON c.id=b.id_c LIMIT 5 # LIMIT 5 Here Does Not Work
WHERE ...
...和SQLFiddle在http://sqlfiddle.com/#!9/c1822/12
此处还有一个我尝试但不起作用的子查询重写,因为无法从嵌套子查询中访问外部表,并且在“where子句”中出现“未知列a.id”失败:http://sqlfiddle.com/#!9/c1822/3
此处还有一个子查询find_in_set rewrite http://sqlfiddle.com/#!9/2d43bb/1,但是对于大型数据集来说效果太慢了。
答案 0 :(得分:2)
您可以使用变量对子查询中的每一行进行编号。然后,您可以根据该数字过滤掉行。此示例使用相同的left join
t1_id
限制为最多3个结果
select *
from table1 t1
left join
(
select @rn := case when t1_id = @prev_id then @rn + 1 else 1 end rn
, @prev_id := t1_id
, t2.*
from table2 t2
order by
t1_id
) t2rn
on t2rn.t1_id = t1.id
and t2rn.rn < 4 -- At most 3 rows
答案 1 :(得分:0)
这是使用变量编写此内容的正确方法。 Andomar的解决方案可能有效,但它有三个问题:
-(UIImage*)normalizeImage:(UIImage*)currentImage {
if (currentImage.imageOrientation == UIImageOrientationUp){
return currentImage;
}
UIGraphicsBeginImageContextWithOptions(currentImage.size, NO, currentImage.scale);
[currentImage drawInRect:CGRectMake(0, 0, currentImage.size.width, currentImage.size.height)];
UIImage *_newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return _newImage;
}
需要位于子查询中。order by
中表达式的评估顺序。因此,您不应在一个表达式中分配变量并在另一个表达式中使用它。所以,更好的版本看起来像:
select
您对select . . .
from table1 t1 left join
(select t2.*,
(@rn := if(@id = t2.t1_id, @rn + 1,
if(@id := t2.t1_id, 1, 1)
)
) as rn
from (select t2.*
table2 t2
order by t1_id
) t2 cross join
(select @rn := 0, @id := -1) params
) t2
on t2.t1_id = t1.id
where t2.rn <= 5; -- At most 5 rows
的使用表明还需要group_concat()
。但是,您的问题似乎更加笼统地限制了行,而且您没有提供有关您实际尝试完成的内容的更多信息。