N
N
Nulltiton2019-12-18 22:31:41
SQL
Nulltiton, 2019-12-18 22:31:41

How should the query be modified so that when fetching all records, the subquery returns one value for each tuple?

I have a query that works fine as long as there is only one entry in the table:

SELECT Waybill.Id
, Waybill.IMO
, Vessel.Name
, Waybill.loginManager
, User.Name
, Waybill.loginStorekeeper
, (SELECT DISTINCT(Name) FROM User, Waybill WHERE (Waybill.loginStorekeeper = User.login)) AS storekeeperName
, Waybill.Date 
FROM Waybill, Vessel, User 
WHERE Waybill.IMO = Vessel.IMO
AND Waybill.loginManager = User.Login
AND Waybill.loginManager = User.Login
ORDER BY Waybill.Id

However, if there are more than one record, then the subquery returns multiple values, which results in an error. Can you please tell me how to fix the request?
Thank you very much in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey c0re, 2019-12-18
@Nulltiton

The subquery that is inserted into the list of columns must return only ONE row.
either rewrite the subquery to return only one row anyway, or join it or the table.
Besides this subquery is not anchored to sampling tables in any way?? as intended?
what is the connection between the tables, what kind of tables? what should be the sample result?
Wangyu, which should be something like this:

SELECT
     w.Id
    ,w.IMO
    ,v.Name
    ,w.loginManager
    ,um.Name AS managerName
    ,w.loginStorekeeper
    ,umk.Name AS storekeeperName
    ,w.Date 
  FROM Waybill w
  INNER JOIN Vessel v ON w.IMO = v.IMO
  INNER JOIN User um  ON w.loginManager = um.Login
  INNER JOIN User umk ON w.loginStorekeeper = umk.Login
  ORDER BY w.Id

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question