webForumDet fria alternativet

Sortera på 2 kolumner

Databaser & SQLur Databashanterare & SQL

2 svar · 404 visningar · startad av Mog

MogMedlem sedan nov. 2003266 inlägg
#1

Jag har en sql-fråga som ser ut så här;

$query = 'SELECT a.*, ' .
	' CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(":", a.id, a.alias) ELSE a.id END as slug,'.
	' CASE WHEN CHAR_LENGTH(cc.alias) THEN CONCAT_WS(":", cc.id, cc.alias) ELSE cc.id END as catslug'.
	' FROM #__content AS a' .
	($show_front == '0' ? ' LEFT JOIN #__content_frontpage AS f ON f.content_id = a.id' : '') .
	' INNER JOIN #__categories AS cc ON cc.id = a.catid' .
	' INNER JOIN #__sections AS s ON s.id = a.sectionid' .
	' WHERE '. $where .' AND s.id > 0' .
	($access ? ' AND a.access <= ' .(int) $aid. ' AND cc.access <= ' .(int) $aid. ' AND s.access <= ' .(int) $aid : '').
	($catid ? $catCondition : '').
	($secid ? $secCondition : '').
	($show_front == '0' ? ' AND f.content_id IS NULL ' : '').
	' AND s.published = 1' .
	' AND cc.published = 1' .
	' ORDER BY '. $ordering;

$ordering innehåller "a.modified DESC, a.created DESC" eller "a.created DESC, a.modified DESC"

Nu vill jag ha ett tredje alternativ där sorteringen blir på båda kolumnerna, som det är nu kommer alla a.created före a.modified vid "a.created DESC, a.modified DESC" även om det finns nyare i a.modified.

Tyckte det borde gå med nån "select as" och sortera på denna men jag får inte ihop det.

@ndersMedlem sedan juni 200032 969 inlägg
#2

Nu vet jag inte vad du använder för dbms, men...

order by case when a.modified > a.created then a.modified else a.created end desc

eller någon liknande variant hade jag försökt mig på.

mvh

MogMedlem sedan nov. 2003266 inlägg
#3

Tackar !
Funkar klockrent.

128 ms totalt · 3 externa anrop · v20260731065814-full.29ac60f6
0 ms — hämta forumlista (cache)
0 ms — hämta statistik (cache)
125 ms — hämta tråd, inlägg och bilagor (db)