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

标签 mysql sql

我一个月前开始学习 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/

相关文章:

sql - SQL Server 2008 中的逗号分割函数

mysql - 如何在 MySql 中的 CONCAT 中使用 GROUP_CONCAT AND SUM VALUES

mysql - 使用 MySql 公式的建议

php - 选择组合框值后填充文本框

php - fatal error : Cannot pass parameter 8 by reference in PHP

sql - 如何基于具有多行作为响应的另一个 SELECT 进行 SELECT?

c# - 将文本 SQL 与 LINQ 结合使用

php - CakePHP 根据 hasMany 关系获取 array() 中的匹配项

sql - 多次选择同一列

mysql - SQL - 表在不同时间点具有不同的值 - 最佳实践?