/    Sign up×
Community /Pin to ProfileBookmark

Hi There

Ok i have a field in my db called sortid and in there i have data like 002a, 0015z,001a, 0015a, 002b

and they are in no order in the db just random in there

i want to use a select statement and sort it

here is my select statemnt

[code=php]“SELECT * FROM tablename ORDER BY (sortid+0) ASC”[/code]

and i get it in this order

001a
002a
002b
0015z
0015a

how do i get it to sort to show
001a
002a
002b
0015a
0015b

HELP ME I’m going crazy LOL?

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@ShrineDesignsDec 12.2004 — try this:SELECT *, CAST(<span><code>sortid</code></span>, UNSIGNED) <span><code>cast</code></span> FROM <span><code>tablename</code></span> GROUP BY <span><code>sortid</code></span> ORDER BY <span><code>cast</code></span> ASC, <span><code>sortid</code></span> ASC
Copy linkTweet thisAlerts:
@RedheadauthorDec 12.2004 — Hey

I tryed this
[code=php]$sql = "SELECT *, CAST(sortid, UNSIGNED) cast FROM tablename WHERE categorie = '$categorie' GROUP BY sortid ORDER BY cast ASC, sortid ASC";[/code]

and i get this error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/site/public_html/folder/filename.php on line 24

what did i do?

?
Copy linkTweet thisAlerts:
@ShrineDesignsDec 12.2004 — try this:SELECT *, <span><code>sortid</code></span> + 0 AS <span><code>num</code></span> FROM <span><code>test</code></span> GROUP BY <span><code>sortid</code></span> ORDER BY <span><code>num</code></span> ASC, <span><code>sortid</code></span> ASC
Copy linkTweet thisAlerts:
@RedheadauthorDec 23.2004 — Thank you for all your help..

I'm sorry i did not get back to you soon, i had the chance to go on a small vacation but wanted to post a reply to say thank you!

and one more thing can you expalin that sql string for me most of it i understand but some of it i do not..
Copy linkTweet thisAlerts:
@ShrineDesignsDec 23.2004 — <span><code>sortid</code></span> + 0 AS <span><code>num</code></span>we add a pseudo-column from "sortid" and force the column to a numeric value and we set the name of the column as "num"GROUP BY <span><code>sortid</code></span>this groups any additional columns to the current result setORDER BY <span><code>num</code></span> ASC, <span><code>sortid</code></span> ASCthis sorts the result set, first by "num", then by "sortid"
×

Success!

Help @Redhead spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...