Ordering MySQL queries by comma-separated strings?
April 9, 2008 9:50 PM
Subscribe
I'm trying to run a MySQL query that returns rows in order by a value stored in a string (but it will also include the non-matching strings). This is not as easy as I would hope. Code inside!
I work at an educational technology consulting company, and i'm making a script in PHP that allows me to track which updates i've completed to various courses. I have a database set up in the following manner:
id----lms----------------------url-------------------------year
1----string(1,5,8)-----------http://blahblah-----------2008
2----(1,4,6)------------------http://blahblah-----------2007
3----(4,8)--------------------http://blahblah-----------2007
Each of the items in the 'lms' section refers to a specific learning management system. I explode the string into an array by breaking it at the comma. The string is created via a series of checkboxes in my app that add a certain number depending on the lms (i.e. 1 = Blackboard, 2 = Plateau, 4 = Moodle).
I can run a MySQL query that sorts by year. It is the following:
$query = "SELECT * FROM courses ORDER BY year";
What I cannot figure out is how to order by which lms is listed in the database. For instance, I want to run a query that is ordered by 4 being in the lms column, so that I see lines 2 and 3 first, and then 1 afterward. Is this possible? Does this even make sense? Thanks.
posted by EduTek to clothing, beauty, & fashion (15 comments total)
4 users marked this as a favorite
posted by EduTek at 9:52 PM on April 9, 2008