0

I want to retrieve some date from 2 different tables action and staff.

This is my code:

SELECT ActionID, FeedbackID, StaffID, ActionDate, ActionDetails,
       (SELECT StaffName, Department 
        FROM staff 
        WHERE staff.StaffID=action.StaffID) 
FROM action;

I am getting this error:

#1241 - Operand should contain 1 column(s)

1

Your error comes from having two outputs in the SELECT sub-query.

Solution: Having 1 sub-query per output should work if each only returns one value.

Warning: If your sub-query returns multiple outputs, you'll receive an error. Thus, it is best practice to use SELECT sub-queries for known single outputs, like COUNT or SUM.

For multiple outputs: Please use joins, for example:

SELECT a.ActionID, a.FeedbackID, a.StaffID, a.ActionDate, a.ActionDetails
, b.staffName, b.department

FROM action a
LEFT JOIN staff b on a.staffid=b.staffid
| improve this answer | |
1

Seems like you want to inner join the two tables on their Primary Keys. Does this run for you:

CODE:

SELECT 
    A.ActionID, A.FeedbackID, A.StaffID, A.ActionDate, A.ActionDetails,
    S.StaffName, S.Department

FROM Action AS A
    INNER JOIN Staff AS S ON Action.ActionID = Staff.ActionID
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.