webForumDet fria alternativet

Sortera på 2 kolumner

Databaser & SQL

2 svar · 406 visningar · startad av Mog

Medlem sedan nov. 2003266 inlägg
Frågan#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.

Medlem sedan juni 200032 967 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

Medlem sedan nov. 2003266 inlägg
#3

Tackar !
Funkar klockrent.

255 ms totalt · 4 externa anrop · v20260731065814-full.1dc6f849
125 ms — deklarationer (db)
0 ms — hämta statistik (cache)
127 ms — hämta tråd, inlägg och bilagor (db)
126 ms — ändringar (db)