PHP Query of Modified mySQL Data Sets

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
cfytable
Forum Commoner
Posts: 29
Joined: Thu May 12, 2005 3:36 pm

PHP Query of Modified mySQL Data Sets

Post by cfytable »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I have first-initial/last-name usernames stored in my database (e.g. jsmith, jdoe). In PHP, I need to display the last names only, which ordinarily would be quite easy with PHP string functions. The rub is that I need the results ordered by the last name, which implies that I need to do a string mod in my query itself. Is this even possible? I've tried something along these lines, but the username field isn't outputting properly (usercomments are getting placed in that spot in my output). Any suggestions?

[syntax="sql"]SELECT SUBSTRING(username,2), userdate, usercomments AS usernameModified, userdate, usercomments FROM mytable ORDER BY usernameModified, userdate

feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

First off, your query renamed usercomments to usernameModified. I'll guess you want something more like this:

Code: Select all

SELECT username, userdate, usercomments FROM mytable ORDER BY SUBSTRING(username, 2), userdate
Post Reply