简体   繁体   中英

Group by several results SQL Oracle

I'd like to have a result grouped by a propertie.

Here's an example about what I would like to retrieve: 在此处输入图片说明

And here's come the table definition : 在此处输入图片说明

I tried this but it does not work :

SELECT      OWNER.NAME, DOG.DOGNAME
WHERE       OWNER.ID = DOG.OWNER_ID
AND         OWNER.NAME = (SELECT OWNER.NAME FROM OWNER);

But it returns me an error:

  1. 00000 - "single-row subquery returns more than one row"

Thanks a lot !

I am not Oracle expert, but I believe you need FROM and JOIN part :-) :

http://sqlfiddle.com/#!4/f8630/1

SELECT      OWNER.ID, OWNER.NAME,
            DOG.ID, DOG.DOGNAME
FROM        OWNER
LEFT JOIN   DOG
ON          OWNER.ID = DOG.OWNER_ID;

根据Alex的响应进行编辑 ,查询的修改版本为:

SELECT OWNER.NAME, DOG.DOGNAME FROM OWNER LEFT JOIN DOG ON OWNER.ID = DOG.OWNER_ID ORDER BY OWNER.NAME

First up, you are missing the table clauses:

And if you put those in, you still have a problem if you have multiple owners with the same name, where the sub-select in the where clause is returning multiple rows:


AND OWNER.NAME = (SELECT OWNER.NAME FROM OWNER);

If NAME is not distinct, this raises the error.

You could change it to:

AND OWNER.NAME IN (SELECT OWNER.NAME FROM OWNER);

Although in this case the entire clause is meaningless as the whole table will always be returned

Final answer:

SELECT      OWNER.NAME, DOG.DOGNAME
FROM        OWNER, DOG
WHERE       OWNER.ID = DOG.OWNER_ID;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM