繁体   English   中英

如何通过另一个表对查询进行排序?

[英]How can I order query by another table?

我有一个很长的查询,但我将其简化:

$query = $this->db->query(" SELECT id FROM oc_products ORDER BY ...? ");

这就是问题所在。 在此表中,我有所有产品,但我有第二个表 oc_seller_products,其中我有相同的列 ID,与 oc_products 匹配。 我想订购第一个没有出现在 oc_seller_products 中的 id,最后是出现在 oc_seller_products 中的 id。

例如:在 oc_products 中我有 ID:1,2,3,4,5

在 oc_seller_products 我只有 ID:4

我需要这样排序:5,3,2,1 最后一个:4

我有一个市场,所以我希望我的产品首先出现在类别页面上,然后是我的卖家产品。

我真的不知道该怎么做。

select op.id
from oc_products op
left join oc_seller_products osp using (id)
order by osp.id is null desc, op.id desc

osp.id is null没有oc_seller_products 记录时将为 1,当有时为 0,因此首先按降序排序。 然后在那之后,在这两个类别中,您似乎想要降序 id 顺序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM