从类型中只获取一行

时间:2012-11-23 16:06:24

标签: sql db2 distinct

我有一个表和其他表日志故障单中的每一行都有许多日志。我想知道某些日志中是否有某个字符串,然后从票证中获取一个寄存器并不重要,因为我只希望每个票证获得一行 。这就是我现在所拥有的:

SELECT ticket.id,ticket.description,ticket.owner 
FROM ticket
JOIN log
ON (log.ticketid=ticket.id)
where log.description LIKE '%someparameter%'

在这种情况下我该怎么做?

3 个答案:

答案 0 :(得分:2)

您可以尝试使用SQL Limit选项吗?

SELECT ticket.id,ticket.description,ticket.owner 
FROM ticket
JOIN log
ON (log.ticketid=ticket.id)
where log.description LIKE '%someparameter%'
LIMIT 1;

答案 1 :(得分:1)

SELECT
  *
FROM
  ticket
WHERE
  EXISTS (SELECT *
            FROM log
           WHERE log.ticketid = ticket.id
             AND log.description LIKE '%someparameter%'

或者...

SELECT
  ticket.id, ticket.description, ticket.owner 
FROM
  ticket
INNER JOIN
  log
    ON (log.ticketid=ticket.id)
WHERE
  log.description LIKE '%someparameter%'
GROUP BY
  ticket.id, ticket.description, ticket.owner 

答案 2 :(得分:0)

通常的DB2等效于LIMIT 1仅为FETCH FIRST 1 ROWS:

SELECT ticket.id,ticket.description,ticket.owner 
FROM ticket
JOIN log
ON (log.ticketid=ticket.id)
where log.description LIKE '%someparameter%'
FETCH FIRST 1 ROWS ONLY

您还可以通过以下方式启用LIMIT语法:

db2set DB2_COMPATIBILITY_VECTOR=4000

或者:

db2set DB2_COMPATIBILITY_VECTOR=MYS