Mysql – Can someone help me with this MySQL code

MySQL

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)

Best Answer

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