是否可以使用子查询但不使用JOIN编写以下查询?

时间:2019-03-28 14:24:33

标签: mysql sql

如果无法在不加入联接的情况下编写以下代码,我会遇到麻烦吗?如果可能的话,有人可以帮助解释和编写没有它的代码吗?

SELECT DISTINCT CONCAT(first_name, ' ', last_name) AS customer_name,
 state AS customer_state
FROM customers c JOIN addresses a
 ON c.customer_id = a.customer_id
ORDER BY last_name, first_name

1 个答案:

答案 0 :(得分:0)

您在这里:

SELECT DISTINCT 
  CONCAT(c.first_name, ' ', c.last_name) AS customer_name,
  (SELECT a.state FROM addresses a WHERE a.customer_id=c.customer_id ) AS customer_state
FROM customers c 
ORDER BY c.last_name, c.first_name