Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 仅显示1个数据_Sql_Oracle_Oracle11g - Fatal编程技术网

Sql 仅显示1个数据

Sql 仅显示1个数据,sql,oracle,oracle11g,Sql,Oracle,Oracle11g,我试图查询数据,但数据超过1。我只想显示1个数据,这是第一个数据 查询: SELECT DISTINCT a.country_id, a.street_address, a.city, a.phone_number, a.location_id, b.ket FROM LOCATIONS a LEFT OUTER JOIN (SE

我试图查询数据,但数据超过1。我只想显示1个数据,这是第一个数据

查询:

SELECT DISTINCT a.country_id,
                a.street_address,
                a.city,
                a.phone_number,
                a.location_id,
                b.ket
  FROM LOCATIONS a
  LEFT OUTER JOIN (SELECT DISTINCT y.country_id,
                                   y.country_name || ' ' || z.REGION_NAME AS ket
                     FROM countries y, regions z
                    WHERE y.REGION_ID = z.REGION_ID) b
    ON a.country_id = b.country_id;

如果您只想从查询中获取第一行,请尝试此操作

 SELECT DISTINCT a.country_id, a.street_address, a.city, a.phone_number, a.location_id, b.ket FROM LOCATIONS a LEFT OUTER JOIN (SELECT DISTINCT y.country_id, y.country_name || ' ' || z.REGION_NAME AS ket FROM countries y, regions z WHERE y.REGION_ID=z.REGION_ID) b ON a.country_id = b.country_id
order by a.country_id
    fetch first 1 rows only;

(我添加了order by子句(假设您需要第一个国家/地区的行)。如果没有order by子句,则不能保证每次都有相同的行。)

您可以使用分析函数,例如
densite\u RANK()
,包括按
国家/地区id进行排序(假定图像中的排序列)为了使第一排(包括领带)如


“第一”与哪个栏有关?您能将示例数据添加到您的问题中吗?如果没有
orderby
,它将是不确定的行。我认为在SQL回答中应该经常说,如果没有显式的
orderby
,表/结果集中就没有第n条记录,你是绝对正确的。我已经更改了答案。
11g
版本没有
fetch
子句。不客气;删除我以前的评论。
SELECT country_id, street_address, city, phone_number, location_id, ket
  FROM
  (
    SELECT l.country_id,
           l.street_address,
           l.city,
           l.phone_number,
           l.location_id,
           c.country_name || ' ' || r.region_name AS ket,
           DENSE_RANK() OVER (ORDER BY l.country_id) AS dr
      FROM locations l
      LEFT JOIN countries c
        ON l.country_id = c.country_id
      LEFT JOIN regions r
        ON c.region_id = r.region_id
    )
  WHERE dr = 1