您是否可以在以下SQL调用中看到限制此特定部分的结果数量的方法:
OR (u1 != '2' AND u2 != '2' AND u3 != '2' AND m.membership_id = 1 AND m.aff IN (SELECT id FROM members WHERE id != 2 AND membership_id = 1 OR jv > 0)))
我想将匹配数限制为一个确切的数字,例如150
完整的SQL是:
SELECT DISTINCT mid, did FROM product_access, members AS m
WHERE mid = m.id AND m.active = 1
AND m.suspended = 0
AND (u1 = '2' OR u2 = '2' OR u3 = '2' OR (u1 != '2' AND u2 != '2' AND u3 != '2' AND m.membership_id = 1 AND m.aff IN (SELECT id FROM members WHERE id != 2 AND membership_id = 1 OR jv > 0))) GROUP BY mid
因此,结果应该包括所有u1 = '2'
,u2 = '2'
所有地方u3 = '2'
所有地方,但只有150所在地:
(u1 != '2' AND u2 != '2' AND u3 != '2' AND m.membership_id = 1 AND m.aff IN (SELECT id FROM members WHERE id != 2 AND membership_id = 1 OR jv > 0))
答案 0 :(得分:2)
您始终可以从查询的每个部分创建子查询,并将其限制为n个最顶层条目。
根据我的理解,这样的事情应该足够了:
SELECT mid, did FROM(
SELECT DISTINCT mid, did FROM product_access, members AS m
WHERE mid = m.id AND m.active = 1
AND m.suspended = 0
AND (u1 = '2' OR u2 = '2' OR u3 = '2')
UNION SELECT TOP 150 DISTINCT mid, did FROM product_access, members AS m
WHERE mid = m.id AND m.active = 1
AND m.suspended = 0
AND (u1 != '2' AND u2 != '2' AND u3 != '2' AND m.membership_id = 1 AND m.aff IN (SELECT id FROM members WHERE id != 2 AND membership_id = 1 OR jv > 0))
) GROUP BY mid
答案 1 :(得分:1)
如果SQL SERVER
然后使用TOP
,请MY SQL
然后使用LIMIT
。
SQL-Server
SELECT TOP 10 * FROM MyTable;
我的SQL
SELECT * FROM MyTable LIMIT 10;