我有以下查询,它提取没有重复的随机行。
我的问题是,当我想为后续页面加载更多时,我该如何避免重复?
SELECT TOP ($limit) Min(c.id) AS id,
s.id AS skid,
s.name,
Min(b.sector) AS sector,
Min(s.currency) AS currency,
Min(s.url) AS url,
Min(sd.description) AS description
FROM coupons AS c
INNER JOIN users AS s
ON c.USER = s.id
INNER JOIN business AS b
ON s.id = b.USER
LEFT JOIN groups AS g
ON g.couponid = c.id
WHERE ( c.[public] = 'True' )
AND ( c.valid = 'True' )
AND ( s.currency = '$curr' )
GROUP BY s.id,
s.name
ORDER BY Newid()
非常感谢
答案 0 :(得分:1)
您必须将返回的记录ID保存在数组中,并在加载新数据时将其作为参数发送。
如果只想用sql解决它,可以创建一个临时表来保存返回的记录ID。当您加载更多数据时,只需添加:
"SELECT TOP ($limit) Min(c.id) AS id,
s.id AS skid,
s.name,
Min(b.sector) AS sector,
Min(s.currency) AS currency,
Min(s.url) AS url,
Min(sd.description) AS description
FROM coupons AS c
INNER JOIN users AS s
ON c.USER = s.id
INNER JOIN business AS b
ON s.id = b.USER
LEFT JOIN groups AS g
ON g.couponid = c.id
WHERE ( c.[public] = 'True' )
AND ( c.valid = 'True' )
AND ( s.currency = '$curr' )
AND ( s.id NOT IN (SELECT id FROM temporary_table))
GROUP BY s.id,
s.name
ORDER BY Newid()";
如果你被允许使用php而不是更容易。只需使用数组变量并在连接到加载页面时发布它。
"SELECT TOP ($limit) Min(c.id) AS id,
s.id AS skid,
s.name,
Min(b.sector) AS sector,
Min(s.currency) AS currency,
Min(s.url) AS url,
Min(sd.description) AS description
FROM coupons AS c
INNER JOIN users AS s
ON c.USER = s.id
INNER JOIN business AS b
ON s.id = b.USER
LEFT JOIN groups AS g
ON g.couponid = c.id
WHERE ( c.[public] = 'True' )
AND ( c.valid = 'True' )
AND ( s.currency = '$curr' )
AND ( s.id NOT IN (" . implode(',', $used_Ids) ."))
GROUP BY s.id,
s.name
ORDER BY Newid()";