我想制作合并2表,然后执行ORDER BY
,首先按top
列排序,以便top
中值为1的产品在记录集中首先显示
之后,我想按premium
列对其进行排序,以便价值为1的产品在top
个产品之后发布。
之后,我希望其余产品从表pub_date
items
开始订购,而highlighted
列中值为1的产品不应直接显示在top
之下}和premium
项。
由于原始表格中包含不必要的信息,因此这里是精简版本:
Table `items`:
+-------+------------+--------+
| pk_id | pub_date | author |
+-------+------------+--------+
| 1 | 2013-06-11 | John |
| 2 | 2013-06-12 | Mike |
| 3 | 2013-06-25 | Seth |
| 4 | 2013-06-11 | Drew |
| 5 | 2013-06-13 | Joe |
+-------+------------+--------+
Table `paid_items`:
+-------+-----+---------+-------------+--------+
| fk_id | top | premium | highlighted | active |
+-------+-----+---------+-------------+--------+
| 2 | 1 | 0 | 0 | 1 |
| 3 | 0 | 0 | 1 | 1 |
| 4 | 0 | 1 | 0 | 1 |
| 5 | 0 | 0 | 1 | 1 |
+-------+-----+---------+-------------+--------+
编辑: 这是我想要完成的伪(或其他):
PRODUCTS_LIST>
PRODUCTS WITH TOP VALUE
PRODUCTS WITH PREMIUM VALUE
PRODUCTS ORDERED BY PUB_DATE>
PRODUCT WITH NO HIGHLIGHTED VALUE
PRODUCT WITH NO HIGHLIGHTED VALUE
PRODUCT WITH HIGHLIGHTED VALUE
PRODUCT WITH NO HIGHLIGHTED VALUE
PRODUCT WITH HIGHLIGHTED VALUE
...
记录集应如何显示的条带化版本,焦点位于highlighted
列:
mysql> select pk_id, highlighted from items left join paid_items on items.pk_id
= paid_items.fk_id order by pub_date desc;
+-------+-------------+
| pk_id | highlighted |
+-------+-------------+
| 3 | 1 |
| 5 | 1 |
| 2 | 0 |
| 1 | NULL |
| 4 | 0 |
+-------+-------------+
答案 0 :(得分:0)
简单选择将是: -
SELECT *
FROM items a
LEFT OUTER JOIN paid_items b
ON a.pk_id = b.fk_id
ORDER BY b.top, b.premium, a.pub_date
但我认为你已经知道了。
但是我不确定产品的含义是什么意思,突出显示的列中值为1的产品不应直接显示在顶级和高级商品之下。您希望这些被排除,还是显示在列表的底部。你能在问题中加上一些示例输出吗?