I’m not sure what is going on here. I have created a MySQL view and to grab some data out of 4 different tables but it only seems to be returning the first record.
CREATE OR REPLACE VIEW request_view AS
SELECT
TRequest.id,
TRequest.minutes_required,
TRequest.expires_at,
User.name AS author,
TRequest.abstract_text,
TRequest.full_text,
TGroup.name AS tgroup,
SUM(TOrder.minutes) AS total_minutes
FROM
TRequest
JOIN User ON TRequest.author_id = User.id
LEFT JOIN TGroup ON TRequest.group_id = TGroup.id
LEFT JOIN TOrder ON TRequest.id = TOrder.request_id
ORDER BY TRequest.created_at;
When I try to select against this view it will only return the first record:
mysql> select id from request_view;
+----+
| id |
+----+
| 1 |
+----+
1 row in set (0.00 sec)
mysql> select id from request_view where id=2;
Empty set (0.00 sec)
I have 30 TRequest rows… is there anyway I can get the view to return all of the TRequest records? Or am I simply using the View incorrectly?
You’re doing a
SUMwithout aGROUP BY.SUMis an aggregate function which only make sense over a group of rows. If you don’t specify any columns to group by, the default is to take the sum over all the rows, which means you only get one result.If you want one row for each column in
TRequest, try addingGROUP BY TRequest.idto your view definition.