SQL连接查询没有公共密钥

时间:2014-12-08 14:31:21

标签: mysql sql join

我有这个查询获取客户列表

SELECT CONCAT(first_name, ' ', last_name) as customer_name, customer_email as customer_email FROM customers WHERE user_level = 'customer')

并且我想要追加此查询的每一行

(SELECT CONCAT(first_name, ' ', last_name) as seller_name, seller_email FROM users WHERE user_level = 'seller')

对于每个客户,卖家将与我在我的表中只有一个卖家相同,所以我想得到这个输出。我怎么能创造这个?

customer_name    |     customer_email    |    seller_name    |     Seller_email
-------------------------------------------------------------------------------
some customer    |   xxxxxxxxxxx         |    this seller    |    seller@email.com
some customer1   |   xxxxxxxxxxx         |    this seller    |    seller@email.com
some customer2   |   xxxxxxxxxxx         |    this seller    |    seller@email.com

1 个答案:

答案 0 :(得分:1)

由于您的users表格中只有一条记录,因此您可以使用cross join来获得所需的结果;

SELECT CONCAT(c.first_name, ' ', c.last_name) as customer_name, 
       c.customer_email as customer_email,
       CONCAT(u.first_name, ' ', u.last_name) as seller_name, 
       u.seller_email 
FROM customers c cross join users u
WHERE c.user_level = 'customer'
      and u.user_level = 'seller'