473,809 Members | 2,719 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

joining results in single record?

I have a table with 2 records:

==========
TABLE: users
==========

name_id (INT)
name (varchar)
code (int)

Inside the table I have a few records:

id name code
-----------------
1. John 233
2. Fred 211
3. John 122
4. John 555
5. Fred 444

(this is a simplified example of the actual tables so bare with me)

Now I want to select all users and COMBINE the codes and INSERT them into a
new table like this:

=============
TABLE: combined
=============

id (INT)
name (varchar)
combined_codes (varchar)

id name combined_codes
-----------------------------
1. John 233, 122, 55
2. Fred 211, 444

I have tried the CONCAT_WS statement, but code like "CONCAT_WS( ',', code,
code) only combines 2 of the same codes.

Is there a way to do this?

Tnx.
Jul 20 '05 #1
1 1585
Ondernemer wrote:
I have a table with 2 records:

==========
TABLE: users
==========

name_id (INT)
name (varchar)
code (int)

Inside the table I have a few records:

id name code
-----------------
1. John 233
2. Fred 211
3. John 122
4. John 555
5. Fred 444

(this is a simplified example of the actual tables so bare with me)

Now I want to select all users and COMBINE the codes and INSERT them into a
new table like this:

=============
TABLE: combined
=============

id (INT)
name (varchar)
combined_codes (varchar)

id name combined_codes
-----------------------------
1. John 233, 122, 55
2. Fred 211, 444

I have tried the CONCAT_WS statement, but code like "CONCAT_WS( ',', code,
code) only combines 2 of the same codes.

Is there a way to do this?

Tnx.


I'd do it in PHP or some other coding language like this:
(1)create a new table (to handle combined situation).
(2)Issue a select distinct on user name + code in your first table.
Order by username.
(3)using PHP, cycle through the set and collect the data with a simple
algorithm. Use a previous/current variable to flag a change in
username. if no change, keep concatenating the $combined_value variable.
(4) when a change occurs, write out a single line: containing the
(previous, before the change)username and concatenated variable to your
new combined table.

There's probably a way to do it with just SQL, but why fry your brain on
that route? My general rule of thumb is that the more complex the
logic, the more it begs to be done outside of SQL. :-)

-Paul Bramscher
Jul 20 '05 #2

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

Similar topics

2
3514
by: James | last post by:
Can anyone please shed some light on the following... I have a framework that uses dynamically created tables, named using an incremental "attribute set ID", as follows: attrdata_1 attrdata_2 attrdata_3 etc, etc...
4
3815
by: Larry | last post by:
OK, I'm just learning MySQL, or at least trying to. I have a table with data as follows USER FIELDID VALUE 1 1 Bob 1 2 Smith 2 1 John 2 2 Smith etc.
2
1299
by: phillip.s.powell | last post by:
SELECT s.id, s.student_first_name, s.student_last_name, IF(s.student_ethnicity_interest_other IS NOT NULL AND s.student_ethnicity_interest_other != '', CONCAT(s.student_ethnicity_interest_other, ',', GROUP_CONCAT(e.ethnicity_name ORDER BY upper(e.ethnicity_name))), GROUP_CONCAT(e.ethnicity_name ORDER BY upper(e.ethnicity_name)) ) AS ethnicity_name
2
5707
by: mdallison | last post by:
How can I join two tables I have and keep the columns independently distinct. For example, say i have the folowing tables: table1: station_id sequence ------------------------------- 1 ---------- 1 1 ---------- 2
1
2489
by: aknoch | last post by:
My basic situation is this - I ONLY want duplicates, so the opposite of DISTINCT: I have two tables. Ordinarily, Table1ColumnA corresponds in a one to one ratio with Table2ColumnB through a shared variable. So if I query TableB using the shared variable, there really should only be on record returned. In essence, if I run this and return TWO rows, it is very bad: select * from TableB where SharedVariable = 1234
1
2541
by: pmouseca | last post by:
Hello all, I have two tables, one call Assignments, the other call Grades. In Assignment, the columns are: Asn_ID, Asn_Name, Asn_MaxGrade In Grades, the columns are: Grade_ID, Grade_Assignment, Grade_Student, Grade_Value There exist a one-to-many relationship on Asn_ID = Grade_Assignment
2
2269
by: Supermansteel | last post by:
I am joining these 2 tables together in Access 2003 and can't figure out the exact way of writing this script......Can anyone help? I have the following SQL: SELECT PL_Input.Date_ID,Count(PL_Input.+PL_Input.+PL_Input.+PL_Input.+PL_Input.) AS FROM Country INNER JOIN PL_Input ON Country.Country_ID = PL_Input.Country_ID WHERE (((. & "" & . & "" & . & "" & . & "" & .) Like "*2*"))
4
1988
by: rhino | last post by:
I'm very new to XML and maybe just a touch impatient because I'm going to ask a moderately advanced question even though I'm just learning the basics. I've spent many years working with databases, both hierarchical and relational. So far, XML is obviously hierarchical in nature. I'm wondering if there is anything analogous to a relational "join" in XML? For example, let's say I have an XML file that has a root element of departments....
3
2850
by: Ken Fine | last post by:
This is a question that someone familiar with ASP.NET and ADO.NET DataSets and DataTables should be able to answer fairly easily. The basic question is how I can efficiently match data from one dataset to data in a second dataset, using a common key. I will first describe the problem in words and then I will show my code, which has most of the solution done already. I have built an ASP.NET that queries an Index Server and returns a...
0
9721
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
10639
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
10376
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
10383
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6881
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
5550
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...
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
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.