473,385 Members | 1,730 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,385 software developers and data experts.

Can I reduce the lines of my MySQL output by concatenation?

Hi All!!!

Let us consider two tables in a MySQL database:

1) C_T is a table of school classes and the teachers that teach in
them:

select * from C_T;

+-------+---------+
| Class | Teacher |
+-------+---------+
| A | John |
| A | Robert |
| B | Julian |
| C | George |
| C | John |
+-------+---------+

2) T_S is a table that shows the subject of each teacher.

select * from T_S;

+---------+-----------+
| Teacher | Subject |
+---------+-----------+
| John | Maths |
| Julian | Chemistry |
| George | Physics |
| Robert | Music |
+---------+-----------+

This is the ERD schema of the MySQL database:

+-----------------+ +-------------+
| C_T | | T_S |
+-----------------+\ +-------------+
| Class PK |-+---+| Teacher PK |
| Teacher PK, FK |/ | Subject |
+-----------------+ +-------------+

As you can see, the tables have an "one to many" relationship.

C_T has a composite primary key (Class and Teacher) and T_S's primary
key is Teacher (which is exported as an identifying foreign key in C_T
table).

If I try:

select C_T.Class, C_T.Teacher, T_S.Subject
from C_T, T_S
where C_T.Teacher=T_S.Teacher;

+-------+---------+-----------+
| Class | Teacher | Subject |
+-------+---------+-----------+
| A | John | Maths |
| A | Robert | Music |
| B | Julian | Chemistry |
| C | George | Physics |
| C | John | Maths |
+-------+---------+-----------+

I can see all the entries of C_T (attributes Class and Teacher)
together with the relevent entries of T_S from attribute Subject.

Is there a way I can get in a SINGLE line ALL the entries of a single
primary key of the first table? In my example, can I get a 3-line
output where each line would contain each Class, the names of the
Teachers separated by a semicolon, and the Subjects taught in the
Class, also separated by a semicolon? In other words, I want to get
the following:

+-------+-------------+---------------+
| Class | Teacher | Subject |
+-------+-------------+---------------+
| A | John;Robert | Maths,Music |
| B | Julian | Chemistry |
| C | George;John | Physics;Maths |
+-------+-------------+---------------+

If there is a way, I would be indebted if you could show it to me.

Regards,

Asteras
Jul 23 '05 #1
1 1136
Asteras wrote:
Is there a way I can get in a SINGLE line ALL the entries of a single
primary key of the first table?


This is a very similar problem to another that just came across this
newsgroup today, under the subject "is this possible with selects and
joins?"

My answer would be very similar: fetch the data in its "expanded" form,
and then do the concatenation in application code.

Regards,
Bill K.
Jul 23 '05 #2

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

Similar topics

15
by: Stat | last post by:
Greetings all. I am writing a profile creator script where a user gets a URL invite in their mail in the form of; http://domain.com/profile-create.php?access_code=Ikd98jadf098asdf Things are...
5
by: Marcel Akkerman | last post by:
Hi, Does anyone have a clue how to reduce the number of nodes using XSLT? When outputing all nodes in order I could just use <xsl:for-each select="name"> But what if I, besides sorting and...
6
by: mr_burns | last post by:
hi, is it possible to have a string variable to contain new lines. i tried the following but it didnt work: var_string = '<h1>Title</h1> <img src="image.gif> <p>Body text</p>'; i know...
10
by: John Smith | last post by:
I know that uploading an image to a database has been covered, oh, about 3 trillion times. However, I haven't found anything covering uploading to a MySQL database with .net. Please don't...
1
by: Karuna | last post by:
Hi, I am new to MySQL. I want to create a script file which will include a number of commands and execute it, just like we do in Oracle. Can any one tell me how to do it? Suppose I want to create...
0
by: IamtheEvster | last post by:
Hi All, I am currently using PHP 5 and MySQL 5, both on Fedora Core 5. I am unable to call a MySQL stored procedure that returns output parameters using mysql, mysqli, or PDO. I'm having a...
2
josie23
by: josie23 | last post by:
Egad, I'm not a coder/programmer by nature or occupation but understand things like html and css and a small amount of perl. So, basically, I'm a perl/mysql imbecile. But, I've been trying to...
3
by: menzies | last post by:
Hi, I"m new to this forum, but I have been trying all day to install DBD::mysql onto my Intel MacBook. I've read lots of forums pages and none have gotten me to a successful 'make test' or a...
1
ssnaik84
by: ssnaik84 | last post by:
Hi Guys, Last year I got a chance to work with R&D team, which was working on DB scripts conversion.. Though there is migration tool available, it converts only tables and constraints.. Rest of...
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:
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.