473,883 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1157
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
1837
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 working well except for a small annoyance in which someone might have a solution to. In the event that someone accesses profile-create.php without an access_code the script generates a warning. If there is an access_code
5
3562
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 grouping, I also output conditionally? <xsl:for-each
6
1941
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 that html will display the same if i was to write the above
10
7418
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 recommend storing the image to the filesystem and only keeping a pointer to that in the table. I want to dump the image to a table. My code dumps the data into the table, however, I get the following error when trying to view the image "the image ......
1
2085
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 a script called "temp" that will contain say 4 create statement. I want someone to show me how to create that file and execute it. And how to capture the output in a separate file like we do in Oracle. And what should be the extension of the...
0
19304
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 hell of a time with it... The following comes from phpinfo(): PHP Version: 5.1.2 mysql Client API version: 5.0.18 mysqli Client API version: 5.0.18
2
7053
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 find syntax to insert values into a mysql database table. I'm able to use the below syntax to insert hard-coded values like 'josie' and 'smith' but can't find working syntax to insert $scalar data from another file (which is really what perl-mysql is...
3
8841
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 successful 'sudo make install.' Before every attempt I even do a sudo make distclean to make sure I haven't gotten things mucked up from a prior attempt. OS: Mac OS X 10.4.10 MySQL: v5.0.41 for Mac OSX-i686 DBI: v1.58 (installed fine using CPAN)...
1
9602
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 things (stored procedures, functions).. we have to manually edit. That time, we face some interesting challenges.. I failed to document all of them, but whatever I can share with u.. I will try.. :) ...
0
9942
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9792
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11144
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10747
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7973
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7133
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5801
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4616
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3235
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.