[英]MySQL Selecting a complex sub query as a field
I currently have: 我目前有:
SELECT tbl_review.*, users.first_name, users.last_name, (
SELECT order_ns.tran_date
FROM order_ns
LEFT JOIN product_2_order_ns.external_order_id = order_ns.order_id
WHERE product_2_order_ns.bkfno IN :id
ORDER BY order_ns.trandate ASC
LIMIT 1
) as purchase_date
FROM tbl_review
LEFT JOIN users ON users.sequal_user_id = tbl_review.user_id
WHERE tbl_review.product_id IN :id AND tbl_review.approved = 1
Which, in its sub query, selects an order the user has which has a product in question (defined in :id
) get the the oldest transaction date on file for one of the found orders. 它在其子查询中选择一个用户所拥有的订单,该订单中有一个有问题的产品(在
:id
定义)获得所找到订单中最旧的交易日期。
I would really like to keep this to one call of the database (don't really want to call again for each returned user for just one field, or even do a range query of all users) but obviously this particular query isn't working. 我真的很想将其保留给数据库的一次调用(不想真的只为一个字段的每个返回用户再次调用,甚至不想对所有用户进行范围查询),但是显然此特定查询无法正常工作。
What can I do, if anything, to get this working? 我要怎么做才能使它正常工作?
I cannot make the sub query into a join since they are two distinct pieces of data, the sub query needs to return detail for each row in the main query. 我无法使子查询成为联接,因为它们是两个不同的数据,子查询需要返回主查询中每一行的详细信息。
I think you just want a correlated subquery. 我认为您只需要一个相关的子查询。 It is unclear exactly what the relationship is between the inner query and the outer one.
目前尚不清楚内部查询和外部查询之间的关系是什么。 My guess is that it is on users and orders:
我的猜测是,它取决于用户和订单:
SELECT tbl_review.*, users.first_name, users.last_name,
(SELECT order_ns.tran_date
FROM order_ns LEFT JOIN
product_2_order_ns
on product_2_order_ns.external_order_id = order_ns.order_id and
product_2_order_ns.bkfno = tbl_review.product_id and
WHERE order_ns.user_id = tbl_review.user_id
ORDER BY order_ns.trandate ASC
LIMIT 1
) as purchase_date
FROM tbl_review LEFT JOIN
users
ON users.sequal_user_id = tbl_review.user_id
WHERE tbl_review.product_id IN :id AND tbl_review.approved = 1;
EDIT: 编辑:
Oh, the inner query has no relationship to the outer query. 哦,内部查询具有外部查询没有关系。 Then it is easier.
那就容易了。 Move it to the
from
clause using cross join
: 使用
cross join
将其移至from
子句:
SELECT tbl_review.*, users.first_name, users.last_name,
innerquery.tran_date as purchase_date
FROM tbl_review LEFT JOIN
users
ON users.sequal_user_id = tbl_review.user_id cross join
(SELECT order_ns.tran_date
FROM order_ns LEFT JOIN
product_2_order_ns
on product_2_order_ns.external_order_id = order_ns.order_id
WHERE product_2_order_ns.bkfno IN :id
ORDER BY order_ns.trandate ASC
LIMIT 1
) innerquery
WHERE tbl_review.product_id IN :id AND tbl_review.approved = 1;
@Gordons answer is really close but I wanted it to return even if no data was found for tran_date so I changed my query to: @Gordons答案非常接近,但是我希望它返回,即使未找到tran_date的数据,因此我将查询更改为:
SELECT tbl_review.*, users.first_name, users.last_name, order_ns.tran_date
FROM tbl_review
LEFT JOIN users ON users.sequal_user_id = tbl_review.user_id
LEFT JOIN order_ns ON order_ns.order_id = (
SELECT order_ns.order_id
FROM order_ns
LEFT JOIN product_2_order_ns on product_2_order_ns.external_order_id = order_ns.order_id
WHERE product_2_order_ns.bkfno IN :id
ORDER BY order_ns.tran_date ASC
LIMIT 1
)
WHERE tbl_review.product_id IN :id AND tbl_review.approved = 1;
This will return the distinct data of tran_date
irrespective of whether it is found or not. 这将返回
tran_date
的不同数据,无论是否找到该数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.