473,385 Members | 1,942 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.

Joining data from 2 fields into the first one

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.

I want to concatenate the first and last names and update the firstname fields
with the full name throughout the whole table. I know how to loop through the
data (in PHP), concatenate them, and write the result back to the first name
field, but I'm trying to learn SQL.

Isn't there an obvious way to accomplish the same thing with just SQL? Somehow
I just can't see it.

Many thanks,
Larry

Mar 24 '06 #1
4 3791
"Larry" <no***@none.com> wrote in message
news:4r*****************@tornado.socal.rr.com...
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.

I want to concatenate the first and last names and update the firstname
fields
with the full name throughout the whole table. I know how to loop through
the
data (in PHP), concatenate them, and write the result back to the first
name
field, but I'm trying to learn SQL.

Isn't there an obvious way to accomplish the same thing with just SQL?
Somehow
I just can't see it.


You need a self-join:

SELECT CONCAT(firstName.value, ' ', lastName.value) AS FullName
FROM myTable AS firstName INNER JOIN myTable AS lastName
ON firstName.user = lastName.user AND firstName.fieldid = 1 AND
lastName.fieldid = 2

However, I'm assuming you have more than just these two fields. You'll find
that you need as many joins as you have fields (minus one) if you store the
fields the way you're storing them. That is, if you need to retrieve all
fields for a given user in one query.

The design you're using is called Entity-Attribute-Value, or EAV. It's
often criticized because it doesn't scale well, and lacks referential
integrity. Aside from the lots-of-joins problem described above, here's
another problem: how can you make sure a given field has a value for each
user?

In a standard table design, you can make the field "NOT NULL" and any
attempt to INSERT or UPDATE the record without supplying a value for that
field results in an error.

In the EAV design, there's no way to enforce it, except by a comparatively
expensive task of querying for the field for that user and making your
application raise an error if the field is absent. Querying must be done
using an outer join:

SELECT CONCAT('User ID ', u.user, ' has no last name!') AS errorString
FROM myTable AS u LEFT OUTER JOIN myTable AS lastName
ON u.user = lastName.user AND lastName.fieldid = 2
WHERE lastName.fieldid IS NULL

Repeat the above test for all mandatory fields.

See also:
http://ycmi.med.yale.edu/nadkarni/In...%20systems.htm
http://classweb.gmu.edu/kersch/inft8...son/JAMIA5.pdf

Regards,
Bill K.
Mar 24 '06 #2
In article <e0********@enews2.newsguy.com>, "Bill Karwin" <bi**@karwin.com> wrote:
"Larry" <no***@none.com> wrote in message
news:4r*****************@tornado.socal.rr.com.. .
OK, I'm just learning MySQL, or at least trying to. I have a table with

big snip
See also:
http://ycmi.med.yale.edu/nadkarni/In...%20systems.htm
http://classweb.gmu.edu/kersch/inft8...son/JAMIA5.pdf

Regards,
Bill K.


Bill, thanks so very much for the informative lesson. I'll definitely check
out the links. I understand what you mean about the structure. As I said I'm a
novice at this, and was trying to keep my login table as compact as possible
and yes there is other info in this table which for most users will be blank,
which is OK, so I was trying to conserve total size.

One learns as one goes, thanks for the lesson!

Larry
Mar 24 '06 #3
Larry wrote:
In article <e0********@enews2.newsguy.com>, "Bill Karwin" <bi**@karwin.com> wrote:
"Larry" <no***@none.com> wrote in message
news:4r*****************@tornado.socal.rr.com. ..
OK, I'm just learning MySQL, or at least trying to. I have a table with


big snip

See also:
http://ycmi.med.yale.edu/nadkarni/In...%20systems.htm
http://classweb.gmu.edu/kersch/inft8...son/JAMIA5.pdf

Regards,
Bill K.

Bill, thanks so very much for the informative lesson. I'll definitely check
out the links. I understand what you mean about the structure. As I said I'm a
novice at this, and was trying to keep my login table as compact as possible
and yes there is other info in this table which for most users will be blank,
which is OK, so I was trying to conserve total size.


When you think about total size, what takes more total space?

1, 1, bob, some, more, data, here,
1 2 smith ,,,,

or
1, bob, smith, some, more, data, here,

??? :) One thing to remember each record will have so many bytes of
record overhead. in this case you saving one byte in the record plus
n-bytes in the index.

Short double records > single record


One learns as one goes, thanks for the lesson!

Larry


Yes, but these are also the lessons you remember :)
Mar 25 '06 #4
I want to concatenate the first and last names and update the firstname fields with the full name throughout the whole table ... Isn't there an obvious way to accomplish the same thing with just SQL?
Yup ... I had a similar situation recently and found that the following worked just fine for me ... I know my post is a little late, but perhaps it may help someone else in the future :)

UPDATE ContactsDatabase SET FirstName = CONCAT(FirstName,LastName)

... of course, you need to replace the DB name and field names with your own.
Apr 7 '06 #5

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

Similar topics

3
by: Ben Willcox | last post by:
Hi I am having difficulty writing an SQL query to do what I want: I have 1 table with 2 columns, 'id' and 'name': tbl_names: id name -- ---- 1 Bob 2 Jeff 3 Fred
2
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...
3
by: Andrew | last post by:
Here's my problem: I built a system for data entry and export based on a schema given to my by the state. The output of the data is fixed-width, and a good number of the fields in each row...
9
by: Eric Sabine | last post by:
Can someone give me a practical example of why I would join threads? I am assuming that you would typically join a background thread with the UI thread and not a background to a background, but...
3
by: VMI | last post by:
I'm currently using the dataAdapter.Fill method to fill my dataset with data from an Access DB. My dataset will contain a table with three fields from Access. In my datagrid (for user...
3
by: Reader | last post by:
Hello all, I am joining two tables in a query and the output should be all those records where two fields in table1 match two corresponding fields in table2. I joined the tables using both...
5
by: mike | last post by:
Question. I have a new table that I am adding to a script that I wrote. This table has 3 fields, the first 2 fields are used in the on statement as being = other fields in the script. The...
4
by: Rnt6872 | last post by:
Table A Table B BOL# B_BOL# Chargeback# Hi All, I have been struggling with this for...
4
by: herath | last post by:
Is there a way to join 2 tables where the 2 fields on which the tables joined are of different data types(char and varchar)?I tried with CONVERT but is does not give the desired output. My...
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
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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,...
0
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...

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.