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

How to find the lowest value from several fields and assign it to another field?

107 100+
i have a table where the records are in the following fashion:

Expand|Select|Wrap|Line Numbers
  1. firm                  w1        w3          w4           class
  2. abc ltd.              b          c          d            b
  3. bcd ltd.              c          c          d            c
  4. cde ltd.              a          d          b            a
  5. def  ltd.             d          d          d            d
as clearly indicated, the highest category among w1,w3 and w4 makes up the class. Here "a" is regarded as the highest category and "b" the next and so on. So among the three categories, i want to take the highest category and assign it to class. I have around 2000 records to which i want an update sql statement so that the each records are assigned with proper class. At present the records in class column is empty in my table.

pls help me to achieve this
Jul 6 '09 #1
9 3382
SLauren
60
SELECT LEAST(ASCII(w1),ASCII(w3),ASCII(w4)) into value FROM Table;

Then convert "value" into charcter again and assign that value to your class field.

SET class = CHAR(val);

you can do like this:
UPDATE Table SET class = CHAR(LEAST(ASCII(w1),ASCII(w3),ASCII(w4)));

Thanks,
Lauren
Jul 6 '09 #2
raamay
107 100+
@SLauren
thankyou Lauren, as your approach for the same seems to work but still i have a problem. That is, i forgot to mention that the w1, w3 and w4 can also contain NULL values. So, if i update it with the above SQL statement, some records are not assigned any class since some categories have "NULL" as values in my mysql table. So, now how should i take care of this NULL problems too?
Jul 6 '09 #3
SLauren
60
Expand|Select|Wrap|Line Numbers
  1. UPDATE Table SET class = CHAR(LEAST(IFNULL(ASCII(w1),ASCII('z')),IFNULL(ASCII(w3),ASCII('z')),IFNULL(ASCII(w4),ASCII('z')));
As we can assume that nothing can be greater than 'z' in the alphabet series :)

Thanks,
Lauren
Jul 6 '09 #4
Atli
5,058 Expert 4TB
You don't really need the ASCII calls tho. The LEAST function compares characters just the same without them ;-)
Jul 6 '09 #5
SLauren
60
Thanks for the optimization, i became little bit more logical :-)

Thanks,
Lauren
Jul 6 '09 #6
raamay
107 100+
@SLauren
there is some problem with the sql statement cause i get the following error. Sorry, i am not good with this type of sql queries. so please help me further.

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
I am trying to run this query via phpmyadmin.
Jul 6 '09 #7
raamay
107 100+
@Atli
thankyou for a change of the thread heading
Jul 6 '09 #8
SLauren
60
UPDATE Table SET class = LEAST(IFNULL(w1,'z'),IFNULL(w3,'z'),IFNULL(w4,'z') );

Sorry i forgot to put a mysterious closing bracket at the end in the above query.

This is the optimized query without using ASCII function.

Thanks,
Lauren
Jul 6 '09 #9
raamay
107 100+
forgot to thankyou for your help. it worked like charm .... so thankyou very much.
Jul 7 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

20
by: | last post by:
If I need to check if a certain value does exist in a field, and return either "yes" or "not" which query would be the most effestive?
0
by: QWERTY | last post by:
--------------Boundary-00=_O5I3QL80000000000000 Content-Type: Multipart/Alternative; boundary="------------Boundary-00=_O5I3LVC0000000000000" --------------Boundary-00=_O5I3LVC0000000000000...
2
by: cs8404 | last post by:
I cannot quite figure out how to accomplish the following results. My table is "Products" with the following fields: ID Item Metal Size Price 1 Ring 18ctGold 4-7 $23.00...
26
by: Brett | last post by:
I have created a structure with five fields. I then create an array of this type of structure and place the structure into an array element. Say index one. I want to assign a value to field3 of...
3
by: Chris | last post by:
Before I started to create table, etc to track unique form field record number assigments I thought I'd check to see if there is now a better way to do this in .NET. I have a parent form (table)...
4
by: Learner | last post by:
Hello, I have database field called 'PullAHead' defined as a bit field. Now if user doesn't pick a 'Yes' or 'No' in the front I need to be able to send a null value into the 'PullAHead' field in...
13
by: PW | last post by:
Hi, This is bizarre. I've check and rechecked the spelling, cut and paste the name of the form (when trying to rename the form) and the control yet I still get a parameter prompt when the form...
6
by: Patrick Fisher | last post by:
Hi I have tables from 12 suppliers each of whom can supply the same part, I need to be able to create a table or query containing a list of suppliers who can supply at the lowest price for each...
8
by: mlwerth | last post by:
Dear Access Group: This is the most basic and most embarrassing of questions, but I cannot find where to change the data type of a text field that I have in Access 2003 to a number field. I've...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.