Display details of the owners and their pets. The report should show the full nane (ie title, forename, surname) of the owner together with the name, type and breed of the pet. The output should be sorted on the owner's surname and the pet's name.
时间: 2024-02-24 15:00:30 浏览: 59
Assuming you have two tables named `owners` and `pets`, with fields as follows:
`owners` table:
- `owner_id` (primary key)
- `title`
- `forename`
- `surname`
`pets` table:
- `pet_id` (primary key)
- `owner_id` (foreign key to `owners.owner_id`)
- `name`
- `type`
- `breed`
You can use the following SQL query to display the details of the owners and their pets, sorted by the owner's surname and the pet's name:
```
SELECT o.title, o.forename, o.surname, p.name, p.type, p.breed
FROM owners o
INNER JOIN pets p ON o.owner_id = p.owner_id
ORDER BY o.surname, p.name;
```
This query uses an `INNER JOIN` to combine the `owners` and `pets` tables on the `owner_id` field. The resulting table includes all fields from both tables. The `ORDER BY` clause is used to sort the results first by the owner's surname, then by the pet's name.
阅读全文