如何编写SQL语句?

时间:2013-03-13 12:29:27

标签: sql derby

我有这张表,我想要一个独特的面包ID,无论哪个先进入表格,我都希望它进入查询结果。

表示在以下场景中我想要这些记录具有以下ID。

58,59,60,61,68,69

id  bread id                                        exception               time
58  ID:D--113042539-54026-1363171110733-3:1:1:1:1   null                    16:33.2
59  ID:D--113042539-54026-1363171110733-3:1:1:1:2   null                    17:18.7
60  ID:D--113042539-54026-1363171110733-3:1:1:1:3   null                    17:18.9
61  ID:D--113042539-54026-1363171110733-3:1:1:1:4   null                    17:19.0
62  ID:D--113042539-54026-1363171110733-3:1:1:1:4   jmsexception            17:20.1
63  ID:D--113042539-54026-1363171110733-3:1:1:1:4   jmsexception            17:20.1
64  ID:D--113042539-54026-1363171110733-3:1:1:1:4   jmsexception            17:20.1
65  ID:D--113042539-54026-1363171110733-3:1:1:1:4   jmsexception            17:20.1
66  ID:D--113042539-54026-1363171110733-3:1:1:1:4   jmsexception            17:20.5
68  ID:D--113042539-54026-1363171110733-3:1:1:1:9   jmsexception            17:21.2
69  ID:D--113042539-54300-1363171861821-3:1:1:1:1   InvalidXPathExpression  22:28.1

1 个答案:

答案 0 :(得分:2)

查询:

<强> SQLFIDDLEExample

SELECT `t1`.*
FROM Table1 t1
WHERE t1.id = (SELECT MIN(t.id)
               FROM Table1 t
               WHERE t.`bread id`= t1.`bread id`)

结果:

| ID |                                      BREAD ID |              EXCEPTION |    TIME |
-----------------------------------------------------------------------------------------
| 58 | ID:D--113042539-54026-1363171110733-3:1:1:1:1 |                 (null) | 16:33.2 |
| 59 | ID:D--113042539-54026-1363171110733-3:1:1:1:2 |                 (null) | 17:18.7 |
| 60 | ID:D--113042539-54026-1363171110733-3:1:1:1:3 |                 (null) | 17:18.9 |
| 61 | ID:D--113042539-54026-1363171110733-3:1:1:1:4 |                 (null) | 17:19.0 |
| 68 | ID:D--113042539-54026-1363171110733-3:1:1:1:9 |           jmsexception | 17:21.2 |
| 69 | ID:D--113042539-54300-1363171861821-3:1:1:1:1 | InvalidXPathExpression | 22:28.1 |