actor
表架构
actor_id | first_name | last_name
我有查询
select a.actor_id, t.name,count(*) from actor a
LEFT JOIN
(select fa.actor_id,fa.film_id,c.name from film_actor fa
LEFT JOIN film_category fc ON fc.film_id=fa.film_id
INNER JOIN category c ON c.category_id=fc.category_id
ORDER BY fa.film_id) t ON t.actor_id=a.actor_id
GROUP BY a.actor_id, t.name
ORDER BY a.actor_id
我收到了字段的结果
actor_id | name | count
我还希望在first_name
表中包含last_name
和actor
,但我无法这样做,因为我只能选择group by子句中的那些列名。我希望{/ 1}} first_name
在选择记录中,而不是将其添加到
我希望我的结果为
last_name
答案 0 :(得分:0)
您可以将列放在GROUP BY
子句中:
SELECT a.first_name, a.last_name, a.actor_id, t.name,count(*)
FROM actor a
LEFT JOIN (
SELECT fa.actor_id,fa.film_id,c.name
FROM film_actor fa
LEFT JOIN film_category fc
ON fc.film_id=fa.film_id
INNER JOIN category c
ON c.category_id=fc.category_id
ORDER BY fa.film_id) t
ON t.actor_id=a.actor_id
GROUP BY a.first_name, a.last_name, a.actor_id, t.name
ORDER BY a.actor_id
您可以使用concat()
或format()
或运算符||
来连接字符串(请参阅String Functions and Operators),例如:
SELECT concat(a.first_name, ' ', a.last_name), a.actor_id, t.name,count(*)
...