我现有的查询工作完美无缺。但是,我有一个新字段,用于确定显示这些结果的区域顺序。
例如,如果用户正在浏览网站1(ID 123),那么所有记录
对于区域123和234和543必须显示,但首先记录区域ID 123,然后根据我现有的顺序记录234和543。变量的区域始终为$AreaID
。
请记住我没有按照该区域过滤记录,我只想将该区域的记录全部放在cont_id
ASC订单
我无法在任何地方找到一个例子,甚至可以给我一个可以进行实验的方向。我想我必须以某种方式单独选择它们?
$sql = "select *
from 202_articles
where cont_date_published <= CURDATE()
and cont_time_published < CURTIME()
and cont_category_id = $TypeID
ORDER by cont_id ASC
LIMIT 25";
任何帮助表示感谢。
答案 0 :(得分:1)
假设您有一个名为AreaID
的列,请先使用案例按该条件排序:
$sql = "select *
from 202_articles
where cont_date_published <= CURDATE()
and cont_time_published < CURTIME()
and cont_category_id = $TypeID
ORDER by
CASE WHEN AreaID = $AreaID THEN 0 ELSE 1 END,
AreaID,
cont_id ASC
LIMIT 25";
答案 1 :(得分:0)
$sql = "select *
from 202_articles
where cont_date_published <= CURDATE()
and cont_time_published < CURTIME()
and cont_category_id = $TypeID
ORDER by cont_id ASC
LIMIT 25";
如果您知道该区域,则可以使用以下查询而不是上述查询。
$sql = "select *
from 202_articles
where cont_date_published <= CURDATE()
and cont_time_published < CURTIME()
and cont_category_id = $TypeID
ORDER by cont_id='$userarea' ASC, cont_id ASC
LIMIT 25";
答案 2 :(得分:0)
您可以使用简单的UNION JOIN
执行此操作,以选择具有特定cont_id
的所有内容,然后按cont_id ASC
SELECT
*
FROM
202_articles
WHERE
cont_date_published <= CURDATE()
AND cont_time_published < CURTIME()
AND cont_category_id = $TypeID
AND cont_id = $AreaID
UNION
SELECT
*
FROM
202_articles
WHERE
cont_date_published <= CURDATE()
AND cont_time_published < CURTIME()
AND cont_category_id = $TypeID
ORDER BY
cont_id ASC
LIMIT 25