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 4 3814
"Larry" <no***@none.com > wrote in message
news:4r******** *********@torna do.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(firstNam e.value, ' ', lastName.value) AS FullName
FROM myTable AS firstName INNER JOIN myTable AS lastName
ON firstName.user = lastName.user AND firstName.field id = 1 AND
lastName.fieldi d = 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.fieldi d = 2
WHERE lastName.fieldi d 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.
In article <e0********@ene ws2.newsguy.com >, "Bill Karwin" <bi**@karwin.co m> wrote: "Larry" <no***@none.com > wrote in message news:4r******* **********@torn ado.socal.rr.co m... 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
Larry wrote: In article <e0********@ene ws2.newsguy.com >, "Bill Karwin" <bi**@karwin.co m> wrote:
"Larry" <no***@none.com > wrote in message news:4r****** ***********@tor nado.socal.rr.c om...
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 :)
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 ContactsDatabas e SET FirstName = CONCAT(FirstNam e,LastName)
... of course, you need to replace the DB name and field names with your own.
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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...
|
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 default (never change) for our
particular organization. On the db end, to save space, rather than
create these columns in the main data tables and simply give them
default values for every record, I created a "defaults" table with
them. One row,...
|
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 since I'm asking in the
first place, assume that assumption to be very assuming.
thanks,
Eric
|
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 interaction), I need to join the data from
these 3 cols into a temporary datacolumn. I can't add this additional
column in the Access table because I'll be duplicating data so I was
thinking of creating a datacolumn in runtime and looping through the filled...
| |
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 fields in design view and the Select
statement in SQL view looks good. The query runs perfectly and shows
the result I want but when I save the query and close it, re-opening in
design view shows the error message "Microsoft Office Access can't
|
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 first field always has data in it, but the 2nd field is sometimes
null.
So my problem is if both fields have data in them and they both match
to the data in the fields that I am linking them to, then it returns
|
by: Rnt6872 |
last post by:
Table A Table B
BOL# B_BOL#
Chargeback#
Hi All,
I have been struggling with this for the past few months. I have two
tables that I'm inner joining on BOL#=B_BOL#. This works fine. Now for
the problem....When there are chargeback# fields associated with B_BOL#
they aren't being captured as additional records. None of my tables
|
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 requirement is to get the minimum (earliest) Posted date along with two other fields.But when i join the two fields I get the duplicate records as well.
Following is my query.
...
|
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...
|
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,...
| |
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,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |