2014-09-04 193 views
-1

我有表MYSQL选择查询使用where子句

  
itemuid | category 
----------------------------- 
1   | 3 
2   | 3 
3   | 21 
4   | 3 
5   | 3 
6   | 21 
7   | 3 
----------------------------- 

我现在想itemuid5前选择itemuidcategory21itemuid是我需要向后搜索的唯一信息。

任何人都可以告诉我selectwhere查询必须看起来像吗?谢谢。

+0

您确定这个例子足以代表当前的问题吗? – Strawberry 2014-09-04 10:26:06

+0

你确定你不是指'itemuid 6之前'吗? (否则,'5'会在哪里神奇地来自哪里?) – 2014-09-04 10:28:40

回答

-2

如果我正确理解你,这应该工作:

SELECT itemuid FROM table_name WHERE category=21 and itemuid < 5 
-1

如果 “前” 是指最大itemuid这仍然是小于5,则:

select max(itemuid) from tablename where itemuid < 5 and category = 21 
0
SELECT itemuid from notablenameinquestion where category=21 AND itemuid < 5 order by itemuid LIMIT 1; 
0
SELECT s.itemuid 
    FROM (SELECT itemuid 
      FROM yourTable 
      WHERE yourConditions 
      ORDER BY category DESC) s 
    WHERE ROWNUM = 1 
+2

无需在MySQL中进行子查询,您可以简单地使用LIMIT 1来获得最高的一个。 Oracle背景? – dwjv 2014-09-04 10:27:10

+0

查询您的信息 – mugiwaradz 2014-09-04 10:28:21

1
SELECT * FROM table 
    WHERE itemuid < 5 AND category = 21 
    ORDER BY itemuid DESC 
    LIMIT 1 
0

如果我理解正确:

Select Top(1) itemid from table_name where categoryid=21 and itemid<5