我有这个问题:
SELECT distinct
num as number,
name as name
from my_table_name
where number = '12345';
这就是结果:
number - name 1. 12345 - mike 2. 12345 - charlie 3. 12345 - jose
当发生这种情况时,我需要一个新的查询(数字重复或重复三次)只显示其中一个。示例:
number - name 12345 - mike
我只需要其中一个;这个职位并不重要。如果找到,请打印并关闭程序,功能或光标。
答案 0 :(得分:1)
相对于您查询的所有数据,Distinct将返回不同的结果。如果您只想要返回一个结果并且您知道所使用的结果是任意的,那么您只需添加a filter based on the row number(具体如何完成取决于您使用的DBMS。)
Oracle示例:
select num as "number",
name as "name"
from my_table_name
where number = '12345'
and rownum = 1; -- just gets the first row.
答案 1 :(得分:0)
使用ROW_NUMBER
分析函数
SELECT *
FROM
(
select id, name, ROW_NUMBER() OVER ( partition by id order by name asc) as seq
from tableA
where number = '12345'
) T
where T.seq =1
答案 2 :(得分:0)
SELECT * from
(SELECT rownum rnum,
num as number,
name as name
FROM my_table_name
WHERE number = '12345' )
WHERE rnum = 1
答案 3 :(得分:0)
如果您不关心哪一个被退回,您为什么要求退回?
但是,要获得单行结果而不管匹配行的数量,您应该使用GROUP BY
和摘要函数:
Select
num as number,
max(name) as name --or min(), or any other summary function that works on this data type
from my_table_name
where num = '12345'
group by num