gpt4 book ai didi

mysql - MySQL 的新手,我坚持进行 SAKILA 练习

转载 作者:行者123 更新时间:2023-11-29 05:51:21 31 4
gpt4 key购买 nike

我一个月前开始学习 SQL,我发现了这个 SAKILA 家庭作业,我认为我应该尝试一下,以测试自己。我有点坚持锻炼,我不知道自己做错了什么。有人可以帮助我吗?

问题是:

Find the names (first and last) of all the actors and costumers whose first name is the same as the first name of the actor with ID 8. Do not return the actor with ID 8 himself. Note that you cannot use the name of the actor with ID 8 as a constant (only the ID). There is more than one way to solve this question, but you need to provide only one solution.

我的代码是:

1.

SELECT customer.first_name,customer.last_name FROM customer

LEFT JOIN actor ON (customer.first_name LIKE actor_id=8)
AND (actor.first_name LIKE actor_id=8)

2.

SELECT customer.first_name,customer.last_name FROM customer

LEFT JOIN actor ON customer_id=actor_id

WHERE (customer.first_name LIKE actor_id=8) AND (actor.first_name LIKE actor_id=8)

我得到了很多返回的名字,但没有一个是正确的。我究竟做错了什么?为什么代码没有返回与 Actor 的 ID 为 8 相同的名字。

最佳答案

由于您需要结合 Actor 和服装的名字,我们将首先使用 UNION 来完成。然后将该结果集与 Actor 8 相匹配以匹配名字。

SELECT s.first_name,s.last_name 
FROM (
SELECT c.first_name,c.last_name
FROM customer c
UNION ALL
SELECT a.first_name,a.last_name
FROM actor a
WHERE a.actor_id != 8
) as s
JOIN actor a8 ON a8.first_name = s.first_name
WHERE a8.actor_id=8

关于mysql - MySQL 的新手,我坚持进行 SAKILA 练习,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53848696/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com