itemuid | category ----------------------------- 1 | 3 2 | 3 3 | 21 4 | 3 5 | 3 6 | 21 7 | 3 -----------------------------
我现在想itemuid
5
前选择itemuid
与category
21
。 itemuid
是我需要向后搜索的唯一信息。
任何人都可以告诉我select
和where
查询必须看起来像吗?谢谢。
itemuid | category ----------------------------- 1 | 3 2 | 3 3 | 21 4 | 3 5 | 3 6 | 21 7 | 3 -----------------------------
我现在想itemuid
5
前选择itemuid
与category
21
。 itemuid
是我需要向后搜索的唯一信息。
任何人都可以告诉我select
和where
查询必须看起来像吗?谢谢。
如果我正确理解你,这应该工作:
SELECT itemuid FROM table_name WHERE category=21 and itemuid < 5
如果 “前” 是指最大itemuid
这仍然是小于5,则:
select max(itemuid) from tablename where itemuid < 5 and category = 21
SELECT itemuid from notablenameinquestion where category=21 AND itemuid < 5 order by itemuid LIMIT 1;
SELECT s.itemuid
FROM (SELECT itemuid
FROM yourTable
WHERE yourConditions
ORDER BY category DESC) s
WHERE ROWNUM = 1
无需在MySQL中进行子查询,您可以简单地使用LIMIT 1来获得最高的一个。 Oracle背景? – dwjv 2014-09-04 10:27:10
查询您的信息 – mugiwaradz 2014-09-04 10:28:21
SELECT * FROM table
WHERE itemuid < 5 AND category = 21
ORDER BY itemuid DESC
LIMIT 1
如果我理解正确:
Select Top(1) itemid from table_name where categoryid=21 and itemid<5
您确定这个例子足以代表当前的问题吗? – Strawberry 2014-09-04 10:26:06
你确定你不是指'itemuid 6之前'吗? (否则,'5'会在哪里神奇地来自哪里?) – 2014-09-04 10:28:40