473,387 Members | 1,515 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Sort (ORDER BY) by a comma-separated adjoined string of related separated fields in MySQL 4.1?

create table table_a (
id int not null,
primary key (id),
a_name varchar(125) not null
);

create table table_b (
id int not null,
primary key (id),
b_name varchar(75) not null
);

create table a_b_assoc (
a_id int not null,
b_id int not null,
primary key (a, b)
);
Ok, you have three tables. You're supposed to be able to not only sort
(ORDER BY) according to a_name, no problem, but you must also have the
ability to sort (ORDER BY) the relationship between table_a and
table_b, that is, say you have this:

insert into table_a (a_name) values ('Phil');

insert into table_b (b_name) values ('programmer');
insert into table_b (b_name) values ('developer');
insert into table_c (c_name) values ('emperor of the known universe');

insert into a_b_assoc (a_id, b_id) values ('1', '1');
insert into a_b_assoc(a_id, b_id) values ('1', '2');
insert into a_b_assoc(a_id, b_id) values ('1', '3');
Here is where the problem lies. I am required to be able to sort by
'Phil', no problem:

ORDER BY upper(a_name) ASC
But how on earth do I sort THIS way:

--PSEUDO CODE, of course
ORDER BY upper('programmer, developer, emperor of the known universe')
ASC
in other words, how do I sort a resultset query where each a_name will
have multiple b_name field values BY their adjoined b_name field values
comma-separated?

I'm sorry if that made no sense but that's as clear as I'm able to make
it.

Phil

Feb 5 '06 #1
9 5280
Probably not the best solution, but the only one awarded to me:

I wrote a "stored procedure", which in reality is a CLI PHP script that
queries the DB, returns a resultset (object array) and manipulates it
into the correct structured order (the ethnicities grouped together
into a singular object for each object in the array) before returning a
serialized object array.

Phil

Feb 5 '06 #2
<ph**************@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
ORDER BY upper('programmer, developer, emperor of the known universe') ASC

in other words, how do I sort a resultset query where each a_name will
have multiple b_name field values BY their adjoined b_name field values
comma-separated?


This might help:

SELECT table_a.*, GROUP_CONCAT(b.b_name) AS joined_b_names
FROM table_a INNER JOIN a_b_assoc USING (a_id)
INNER JOIN table_b USING (b_id)
GROUP BY a_id
ORDER BY joined_b_names ASC

See http://dev.mysql.com/doc/refman/5.0/...functions.html for docs
on the GROUP_CONCAT() function.

Regards,
Bill K.
Feb 5 '06 #3
Sorry I tried GROUP_CONCAT, and each time I did the grouping of the
b_name field values were completely wrong, it would seem to group them
almost randomly as opposed to the correct grouping using a_id.

Phil

Bill Karwin wrote:
<ph**************@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
ORDER BY upper('programmer, developer, emperor of the known universe') ASC

in other words, how do I sort a resultset query where each a_name will
have multiple b_name field values BY their adjoined b_name field values
comma-separated?


This might help:

SELECT table_a.*, GROUP_CONCAT(b.b_name) AS joined_b_names
FROM table_a INNER JOIN a_b_assoc USING (a_id)
INNER JOIN table_b USING (b_id)
GROUP BY a_id
ORDER BY joined_b_names ASC

See http://dev.mysql.com/doc/refman/5.0/...functions.html for docs
on the GROUP_CONCAT() function.

Regards,
Bill K.


Feb 6 '06 #4
<ph**************@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Sorry I tried GROUP_CONCAT, and each time I did the grouping of the
b_name field values were completely wrong, it would seem to group them
almost randomly as opposed to the correct grouping using a_id.


There is an option to specify an ordering of the items concatenated.

I'm detecting with you a trend that when I give pointers to pages from the
documentation, you apparently do not read them. You really should really
follow up and supplement advice from newsgroups with some self-education.

Regards,
Bill K.
Feb 6 '06 #5
NO, I do read them, but often times don't understand them, or not
enough to know how to apply them. I can't seem to convey that to
people unless they understand the effects of ADD and know how it can
hamper learning.

Phil

Bill Karwin wrote:
<ph**************@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Sorry I tried GROUP_CONCAT, and each time I did the grouping of the
b_name field values were completely wrong, it would seem to group them
almost randomly as opposed to the correct grouping using a_id.


There is an option to specify an ordering of the items concatenated.

I'm detecting with you a trend that when I give pointers to pages from the
documentation, you apparently do not read them. You really should really
follow up and supplement advice from newsgroups with some self-education.

Regards,
Bill K.


Feb 6 '06 #6
<ph**************@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
NO, I do read them, but often times don't understand them, or not
enough to know how to apply them. I can't seem to convey that to
people unless they understand the effects of ADD and know how it can
hamper learning.


Well, yes, that does sound like quite a difficult challenge. A programming
career is full of daily requirements to learn new tools and techniques.

The GROUP_CONCAT function allows you to specify the order of the strings
concatenated. Otherwise, the order may be dependent on how the records are
stored, which makes it effectively unpredictable.

Specifying the sorting order of strings with GROUP_CONCAT is done with
similar syntax to the order of rows in the query result set. Use an ORDER
BY clause, inside the GROUP_CONCAT function call.

For example:

SELECT a.*, GROUP_CONCAT(b.b_name ORDER BY b.b_name) AS joined_b_names
FROM table_a AS a INNER JOIN a_b_assoc USING (a_id)
INNER JOIN table_b AS b USING (b_id)
GROUP BY a.a_id
ORDER BY joined_b_names ASC

Regards,
Bill K.
Feb 6 '06 #7
Ok, thanx that is a bit more clear, unfortunately, I still can't get it
right, produces a syntax error:

'INNER JOIN student_ethnicity_interest_assoc seia USING
(seia.student_id) ' .
'INNER JOIN ethnicity e USING (e.id) ' .

produces

Fatal error: You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to
use near '(seia.student_id) INNER JOIN ethnicity e USING (e.id) WHERE
MATCH(s.major, s.minor) ' at line 1

Phil
Bill Karwin wrote:
<ph**************@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
NO, I do read them, but often times don't understand them, or not
enough to know how to apply them. I can't seem to convey that to
people unless they understand the effects of ADD and know how it can
hamper learning.


Well, yes, that does sound like quite a difficult challenge. A programming
career is full of daily requirements to learn new tools and techniques.

The GROUP_CONCAT function allows you to specify the order of the strings
concatenated. Otherwise, the order may be dependent on how the records are
stored, which makes it effectively unpredictable.

Specifying the sorting order of strings with GROUP_CONCAT is done with
similar syntax to the order of rows in the query result set. Use an ORDER
BY clause, inside the GROUP_CONCAT function call.

For example:

SELECT a.*, GROUP_CONCAT(b.b_name ORDER BY b.b_name) AS joined_b_names
FROM table_a AS a INNER JOIN a_b_assoc USING (a_id)
INNER JOIN table_b AS b USING (b_id)
GROUP BY a.a_id
ORDER BY joined_b_names ASC

Regards,
Bill K.


Feb 6 '06 #8
<ph**************@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
Fatal error: You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to
use near '(seia.student_id) INNER JOIN ethnicity e USING (e.id) WHERE
MATCH(s.major, s.minor) ' at line 1


The USING clause is useful when you're joining tables and the column has an
identical name in both tables in the join. That, a column by that name has
to appear in both tables.

For example, this clause:

FROM table_a INNER JOIN table_b USING (id)

is the same as this:

FROM table_a INNER JOIN table_b ON table_a.id = table_b.id

If the columns over which you join have different names in each table, you
must use the ON syntax, and write an expression.

Regards,
Bill K.
Feb 7 '06 #9
I did, and used ON instead, which worked, until the elements in
"ethnicity_name" formed by GROUP_CONCAT() changed orderly randomly, and
since I have a demo at 11am tomorrow, I didn't have time to fix that
and the search capability and the customer feedback at the same time,
so I gave up and went back to the "stored procedure" model, which works
perfectly, even if not pretty.

And being at work for 12 hours for the sixth day in a row doesn't help
either. :(

Thanx though!
Phil

Bill Karwin wrote:
<ph**************@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
Fatal error: You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to
use near '(seia.student_id) INNER JOIN ethnicity e USING (e.id) WHERE
MATCH(s.major, s.minor) ' at line 1


The USING clause is useful when you're joining tables and the column has an
identical name in both tables in the join. That, a column by that name has
to appear in both tables.

For example, this clause:

FROM table_a INNER JOIN table_b USING (id)

is the same as this:

FROM table_a INNER JOIN table_b ON table_a.id = table_b.id

If the columns over which you join have different names in each table, you
must use the ON syntax, and write an expression.

Regards,
Bill K.


Feb 7 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Reuben Pearse | last post by:
Hi all, I ran the myisamchk tool with the options --sort-index --sort-records=1 against an MYI file. Is there something I can use to confirm what this command did? I would like to see what the...
2
by: Alpay Eno | last post by:
Hello all... I'm using asp to get records from an access database, very similar to the way datagrid would work. The title of each column in my table is a link that alternates the sort order between...
11
by: James P. | last post by:
Hello, I have a report with the Priority field is used as sort order and grouping. The problem is the data in this Priority field if sorted in ascending order is: High, Low, and Medium. How...
1
by: fecket | last post by:
The following code is used in my db to change the sort order of the report to various fields. I want to take this one stage further and use maybe a Case Select to give 2 or 3 different options to...
1
by: melanieab | last post by:
Hi, I'm not working with anything web-related, all that I have is a datagrid, a dataset, and a datatable. I programatically sort whichever column when I need to. Now I'd like to be able to...
7
by: Steve Crawford | last post by:
I am suffering some sort order confusion. Given a database, "foo", with a single character(4) column of data left padded with spaces I get: select * from foo order by somechars; somechars...
0
by: johkar | last post by:
I have a vendor application that is returning nodes as shown below. The String nodes actually equate to something more user friendly like Employee and Customer. Is there any way to provide a...
9
by: incredible | last post by:
how to sort link list of string
1
by: Pacific Fox | last post by:
Hi all, I have a SQL statement that allows paging and dynamic sorting of the columns, but what I can't figure out without making the SQL a dynamic string and executing it, or duplicating the SQL...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.