[英]mysql using max as where condition
I got 4 fields 我有四个领域
page_id
page_category_id
page_sequence
page_path
For example 例如
record 1
page_id = 1
page_category_id = 22
page_sequence = 1
page_path = "1.jpg"
record 2
page_id = 1
page_category_id = 22
page_sequence = 2
page_path = "2.jpg"
record 3
page_id = 1
page_category_id = 23
page_sequence = 1
page_path = "23_1.jpg"
record 4
page_id = 1
page_category_id = 23
page_sequence = 2
page_path = "23_1.jpg"
record 5
page_id = 1
page_category_id = 23
page_sequence = 3
page_path = "23_1.jpg"
.. and more records
I want to do a draw statement that 我想做一个平局声明
$sql_select = "select page_category_id from my_table where max(page_sequence) < 3";
it will show all the page_category_id who max(page_sequence) is less than 3. 它会显示所有max(page_sequence)小于3的page_category_id。
but I can't do it with max(page_sequence) as the where condition 但我不能使用max(page_sequence)作为where条件
How can I get it working.. Thanks ! 我如何使它工作.. 谢谢 !
select page_category_id from my_table
GROUP BY page_category_id
HAVING max(page_sequence) < 2
SQL is such fun, since you can almost always solve things in several completely different ways. SQL非常有趣,因为您几乎总是可以用几种完全不同的方式解决问题。 Here's a
NOT EXISTS
alternative: 这是一个
NOT EXISTS
选择:
select distinct page_category_id
from tablename t1
where not exists (select 1 from tablename t2
where t2.page_category_id = t1.page_category_id
and t2.page_sequence >= 3)
Return a page_category_id if there are no row with same page_category_id that has a page_sequence >= 3. 如果不存在具有相同page_category_id且page_sequence> = 3的行,则返回page_category_id。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.