M
M
miloh318352021-01-09 17:57:54
MySQL
miloh31835, 2021-01-09 17:57:54

MySQL, how to display fields from different tables using scalar subqueries?

How can I display the number of flights and the person's name if they are in different tables?
1 task for self-
checking You can't merge tables

Here is the task itself
5ff9c35b06899805813285.png

This is the database schema
5ff9c3969068a520688805.png

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Andrey Ezhgurov, 2021-01-09
@miloh31835

What if the passenger checked in but did not buy tickets?
And if several passengers have the same name?
This is how you can do it with table joins:

SELECT COUNT(pit.id) AS count, MAX(p.name) AS name
FROM Passenger AS p
LEFT JOIN Pass_in_trip AS pit ON p.id = pit.passenger
GROUP BY p.id

And like this without joining, in a nested query:
SELECT (SELECT COUNT(pit.id) FROM Pass_in_trip AS pit WHERE p.id = pit.passenger) AS count, p.name
FROM Passenger AS p

G
galaxy, 2021-01-09
@galaxy

The topic "Nested SQL queries" does not say anything?

L
Lazy @BojackHorseman MySQL, 2021-01-09
Tag

what is the meaning of copy-paste in this case? turn on the brain
error in unrecorded namesakes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question