灵活的搜索查询使用三个表

Use*_*413 2 hybris

我有三个表订单,客户,地址.

编辑:

我想要获取所有与网站注册的客户.如果客户已经下了任何订单,我想从订单列表中获取最新订单,如果客户没有下订单,那么我想将该字段设为空白,如下表所示

For eg :

       Customer_Name   Email ID   Country   Phone   Latest_Order_Code
       A               xxxx        xxxx     x       1234 

       B               yyyy        yyyy     y

       C               ffff        tttt     l       3456

       D               zzzz        iiii     o
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激?

Hyb*_*elp 5

请参阅下面的查询,其中我只有获取订单代码和客户名称.您可以根据需要编写更多联接和选择字段.

select {o.code} as orderCode,
       {c.name} as name,
       {a.cellphone} as cellphone

from   {order as o 
        join Customer as c on {c.pk} = {o.user} 
        join Address as a on {o.deliveryaddress} = {a.pk}
       } 

       where {o.code} in ({{select max({code}) from {order} group by {user}}})
Run Code Online (Sandbox Code Playgroud)

更新:获取所有注册客户及其最后的订单信息

select t1.name, t2.orderCode, t2.cellphone

from 

({{
  select {pk} as userPk, {name} as name from {Customer}
}}) as t1

LEFT JOIN

({{
    select 
       {o.code} as orderCode,
       {o.user} as user,
       {a.cellphone} as cellphone

from   {order as o 
        join Address as a on {o.deliveryaddress} = {a.pk}
       } 

       where {o.code} in ({{select max({code}) from {order} group by {user}}})
}}) as t2

on t2.user = t1.userPk
Run Code Online (Sandbox Code Playgroud)

详细介绍如何使用动态表编写复杂灵活的搜索查询连接?