我有一个表格,其结构如下:
id -int(11)
event_id -int(11)
photo_id -int(11)
created_at -datetime
我如何写一个回最近100个牢房的问询,但保证光电灯的连续浏览量不超过4个。
我有一个表格,其结构如下:
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)
我的后勤部无法肯定这一点。
如果您使用T-SQL,请查看用于排名功能。
从你的问题来看,它像NTILE是你想要的。 在我迅速试图盘问时,我不停在终点站,以免检查,而是应该开始:
SELECT
id,
event_id,
photo_id,
created_at,
NTILE(4) OVER (ORDER BY photo_id) AS Quartile
FROM tbl
WHERE NTILE(4) OVER (ORDER BY photo_id)<2
ORDER BY created_at DESC
链接网页有良好的例子说明所有排名的职能。
uck
为此:
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”将只收回分流,无视重复的分行。
希望会有所助益。
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 ...
<?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 = ...
我把我的用心从使用QQL转向MySQL。 它与凯科特合作,现在不工作,因为我已经改变,使用MySQL。 这里的错误信息是:
We have a restaurant table that has lat-long data for each row. We need to write a query that performs a search to find all restaurants within the provided radius e.g. 1 mile, 5 miles etc. We have ...
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 ...
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 ...
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 ~...
My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...