我想加入两个表格(sales_flat_order
& sales_flat_order_address
)。我想使用一个表中的entity_id
与另一个表中的parent_id
匹配。现在每entity_id
有一行,但每个parent_id有两行。但我只想在加入后有一行。
我正在使用这个Statement但是在连接之后它仍然为每个entity_id返回两行。
select o.customer_email, a.prefix
from sales_flat_order o
left JOIN sales_flat_order_address a on o.entity_id = a.parent_id
有人有想法吗?
答案 0 :(得分:0)
只需添加一个不同的?
select DISTINCT o.customer_email, a.prefix
from sales_flat_order o
left JOIN sales_flat_order_address a on o.entity_id = a.parent_id
显然这只会删除多个行,其中o.customer_email,a.prefix实际上是相同的,否则你需要找到一些方法来决定你真正想要的记录......例如:
select o.customer_email, MIN(a.prefix)
from sales_flat_order o
left JOIN sales_flat_order_address a on o.entity_id = a.parent_id
group by o.customer_email
答案 1 :(得分:0)
select o.customer_email,
a.prefix
from sales_flat_order o
left JOIN (Select distinct a.prefix, a.parentid
from sales_flat_order_address) a
on o.entity_id = a.parent_id
答案 2 :(得分:0)
SELECT o.customer_email, oa.prefix
FROM sales_flat_order o
LEFT JOIN
sales_flat_order_address oa
ON oa.entity_id =
(
SELECT entity_id
FROM sales_flat_order_address oai
WHERE oai.parent_id = o.entity_id
ORDER BY
FIND_IN_SET(address_type, 'billing,shipping')
LIMIT 1
)
通过这种方式,您将从帐单邮寄地址(如果已设置)获取前缀,如果未设置帐单邮寄地址,则会返回到送货地址。