我有如下数据库表。
id, bungalow_name, type, address, featured
主页上可以看到平房。如果有简易别墅,featured
列的值为1
。我在桌子上有50个平房,在给定时间有5-7个平房。
我们假设特色平房名称如下。
bungalow 1, bungalow 2, bungalow 3, .........., bungalow 6
我想要做的是在每天的主页上展示一个精选的平房。我希望每个月循环如下所示。鉴于我不想为每个页面加载随机显示一个平房。我想每天展示一个平房。
today -> bungalow 1
tomorrow -> bungalow 2
day after tomorrow -> bungalow 3
...
After bungalow 6, bungalow 1 is shown on the next day.
我该怎么办?甚至可以使用SQL / PHP吗?
答案 0 :(得分:8)
您可以使用此MySQL查询:
SELECT *
FROM Bungalows
WHERE id = (
SELECT b1.id
FROM
Bungalows b1 LEFT JOIN Bungalows b2
ON b1.id>b2.id AND b2.featured=1
WHERE
b1.featured=1
GROUP BY
b1.id
HAVING
COUNT(b2.id) = (SELECT
DATEDIFF(CURDATE(), '2013-05-06') MOD
(SELECT COUNT(*) FROM Bungalows WHERE Featured=1))
)
请参阅小提琴here。 '2013-05-06'是您想要开始展示第一个特色平房的日子。它们将按ID排序,从“2013-05-06”开始。
修改强>
以下查询将返回自2013-05-06以来经过的天数:
SELECT DATEDIFF(CURDATE(), '2013-05-06')
MOD函数将返回经过的天数除以特征行数的整数余数:
SELECT DATEDIFF(CURDATE(), '2013-05-06') MOD
(SELECT COUNT(*) FROM Bungalows WHERE Featured=1)
如果有6个特色平房,它将在第一天返回0,在第二天返回1,再返回2,3,4,5,然后再返回0,1,2 ......
MySQL没有返回RANK(行数)的函数,所以你必须以某种方式模拟它。我这样模仿:
SELECT b1.id, COUNT(b2.id)
FROM
Bungalows b1 LEFT JOIN Bungalows b2
ON b1.id>b2.id AND b2.featured=1
WHERE
b1.featured=1
GROUP BY
b1.id
我正在加入Bungalows桌子。平房ID的等级是ID低于平均值的平房的数量(因此加入b1.id> b2.id)。
然后我只选择上面函数返回的RANK行:
HAVING COUNT(b2.id)=(选择 DATEDIFF(CURDATE(),'2013-05-06')MOD (SELECT CAUNT(*)FROM Bungalows WHERE Featured = 1))
如果您使用MySQL,我的初始查询可以简化为:
SELECT b1.*
FROM
Bungalows b1 LEFT JOIN Bungalows b2
ON b1.id>b2.id AND b2.featured=1
WHERE
b1.featured=1
GROUP BY
b1.id
HAVING
COUNT(b2.id) = (SELECT
DATEDIFF(CURDATE(), '2013-05-06') MOD
(SELECT COUNT(*) FROM Bungalows WHERE Featured=1))
答案 1 :(得分:3)
$dbh = new PDO(....); // use your connection data
$statement = $dbh->query("SELECT count(*) as size FROM bungalows where features = 1");
$data = $statement->fetchALL(PDO::FETCH_CLASS,"stdClass");
$i = date('z') % $data[0]->size;
$statement = $dbh->query("SELECT * FROM bungalows where features = 1 order by id LIMIT $i,1");
$bungalow = reset($statement->fetchALL(PDO::FETCH_CLASS,"stdClass"));
修改强>
答案 2 :(得分:3)
尝试此查询它会在每种情况下都有效,特色平房的数量会增加等 每天会给出不同的一个。
在查询中,我为每个特色平房分配数字,从0到n,然后通过将特色平房的总数除以日期差异,我找到要显示的平房。
查询1 :
select
a.*
from
(select
@rn:=@rn+1 as rId,
b.cnt,
a.*
from
Bunglows a
join
(select @rn:=-1) tmp
join
(select
count(*) as cnt
from
Bunglows
where
featured=1)b
where
featured=1) a
where
datediff(CURDATE(), '2013-01-01')%a.cnt=a.rId
<强> SQL FIDDLE 强>:
| RID | CNT | ID | BUNGALOW_NAME | FEATURED |
---------------------------------------------
| 3 | 4 | 6 | bungalow 4 | 1 |
select count(*) as cnt from Bunglows where featured=1
此查询查找特色平房
select @rn:=@rn+1 as rId, b.cnt, a.* from Bunglows a join (select @rn:=-1) tmp join select count(*) as cnt from Bunglows where featured=1
此查询为每个特色平房添加一个rownumber,从0到n
主查询首先从当前日期和旧日期查找日期差异,并通过总特色平房查找mod值,它将给出从0到n-1的值,并且我添加了一个where子句,用于检查分割值是否为等于我们分配的rowid ..
希望这会有所帮助......
答案 3 :(得分:2)
我的答案的基本概念是:
1 /创建所有特色平房的列表。这是在子查询中实现的,其中每个平房都被赋予唯一的序列号。 seq_num字段的代码基于答案here
2 /根据我们当月的位置选择一个单独的平房。为了做到这一点,我使用代码day(curdate())
查看今天的当天,并找到该数字的mod到特色平房的总数。
select sq.bungalow_name
from (
select bungalow_name
,@curRow := @curRow + 1 AS seq_num
from table1, (SELECT @curRow := 0) r
where featured = 1
order by
bungalow_name desc
) sq
where sq.seq_num = mod(day(curdate()),(select count(*) from table1 where featured = 1))
的示例
答案 4 :(得分:0)
您必须跟踪每个特色记录的最后显示日期(last_viewed
)。
对于新记录,请将此日期设置为过去的某一天,例如。 2000-01-01。
如果有当前日期的记录,请使用该记录。
如果没有,请使用最早日期的记录。
SELECT *, IF(DATE(last_viewed)=CURDATE(), 1, 0) AS current
FROM #__bungalows
WHERE featured=1
ORDER BY current DESC, last_viewed ASC
LIMIT 0,1
与hit
中的com_content
计数器一样,您应该在平房模型中添加点击方法,将所选平房的last_viewed
列设置为now()
。< / p>
答案 5 :(得分:0)
select ....
where featured = 1
limit DAYOFYEAR(NOW()) % (select count(*) from ... where featured = 1), 1
我不确定是否允许subselect限制。您可能必须单独执行该查询。这将每天轮换。容易腻的柠檬挤压。
编辑:执行2次查询
$query = "SELECT COUNT(*) FROM ... WHERE FEATURED = 1";
$count = intval(array_pop(mysql_fetch_assoc(mysql_query($query))));
$query = "
select ....
where featured = 1
limit DAYOFYEAR(NOW()) % {$count}, 1
";
DONE!
答案 6 :(得分:0)
模运算符%可以解决问题:
首先,添加一个“counter int”列。
接下来,对1,2,3等特色列进行编号...如果你很懒,可以使用:
set @cc=0;
update bungalows set counter=(select @cc:=@cc+1) where featured=1;
现在,所有内容都已准备就绪,您只需先进行选择:
select * from bungalows where featured=1 and counter%(select count(*) from bungalows where featured=1)=0;
每次您需要下一个特色平房之前,请执行以下操作:
update bungalows set counter=counter+1 where featured=1;
然后再说:
select * from bungalows where featured=1 and counter%(select count(*) from bungalows where featured=1)=0;
...
update bungalows set counter=counter+1 where featured=1;
依旧......
答案 7 :(得分:0)
点击此处SQLFiddle
SELECT *
FROM bungalows b
JOIN (SELECT
( DAYOFMONTH(CURDATE() ) % COUNT(b2.id) ) AS slab,
COUNT(b2.id) AS total_count
FROM bungalows b2
WHERE b2.featured = 1) AS b3
WHERE IF(b3.slab = 0, b3.total_count, b3.slab) = (SELECT
COUNT(id)
FROM bungalows b1
WHERE b.id >= b1.id
AND b1.featured = 1)
AND b.featured = 1