English 中文(简体)
a ql query中一栏的连续数值限制
原标题:limiting consecutive values of one column in a sql query

我有一个表格,其结构如下:

id           -int(11)
event_id     -int(11)
photo_id     -int(11)
created_at   -datetime

我如何写一个回最近100个牢房的问询,但保证光电灯的连续浏览量不超过4个。

最佳回答

您可以添加一条规定,将4个排位排出,低于photo_id。 存在:

select *
from YourTable t1
where 4 > (
    select count(*)
    from YourTable t2
    where t1.event_id = t2.event_id
    and t1.photo_id < t2.photo_id
)
limit 100

这对庞大的表格来说可能会有某种缓慢。 一个更快但非常具体的MySQL方案是使用变量。 例如:

select *
from (
    select
        @nr := case 
            when event_id = @event then @nr + 1 
            else 1 
        end as photonr
    ,   @event := event_id
    ,   t1.*
    from YourTable as t1
    cross join (select @event := -1, @nr := 1) as initvars
    order by event_id
) as subquery
where subquery.photonr < 5
limit 100;

使用的测试数据:

drop table if exists YourTable;

create table YourTable (
  id int auto_increment primary key
, event_id int
, photo_id int
);

insert into YourTable (event_id, photo_id)
values (1,1), (1,2), (1,3), (1,4), (1,5), (2,1), (1,6);
问题回答

在矿石中,你将使用滞后功能。

LAG  (value_expression [,offset] [,default]) OVER ([query_partition_clause] order_by_clause)

我的后勤部无法肯定这一点。

为此:

SELECT p.id, p.event_id, p.photo_id, p.created_at
FROM photo_table p,
    (

        SELECT photo_id, MAX(created_at) max_date
        FROM photo_table
        GROUP BY photo_id 
    ) t
WHERE p.created_at = t.max_date
        AND p.photo_id = t.photo_id
ORDER BY p.created_at DESC
LIMIT 100

What it does is: 1. find latest photo change date 2. find only last events of each photo 3. select first 100 most recent

在PogreSQL或Oracle,使用Aalytica/windowing职能,如:

FIRST (created_at) OVER (PARTITION BY photo_id ORDER BY created_at DESC)

我这样说,你将走上正轨:

$sql = "SELECT DISTINCT * FROM myTable ORDER BY id ASC LIMIT 100";

在本案中,“DISTINCT”将只收回分流,无视重复的分行。

希望会有所助益。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签