473,471 Members | 1,881 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Saving time by using MySQL query command and the WHERE function

7 New Member
I have a database with some products in it. I can update one of the
products with a SQL query by the command

Expand|Select|Wrap|Line Numbers
  1. UPDATE `mytablename` SET `price` = '7.99' WHERE `id` =26 LIMIT 1 ;

In order for me to update about 100 products to a price '3.99' I have to
go in excel and do some concatenations(linking) and then copy and paste a hundred
individual command lines of

Expand|Select|Wrap|Line Numbers
  1. UPDATE `mytablename` SET `price` = '3.99' WHERE `id` =26 LIMIT 1 ;
  2. UPDATE `mytablename` SET `price` = '3.99' WHERE `id` =27 LIMIT 1 ;
  3. UPDATE `mytablename` SET `price` = '3.99' WHERE `id` =28 LIMIT 1 ;
  4. UPDATE `mytablename` SET `price` = '3.99' WHERE `id` =29 LIMIT 1 ;
  5. UPDATE `mytablename` SET `price` = '3.99' WHERE `id` =30 LIMIT 1 ;
  6. UPDATE `mytablename` SET `price` = '3.99' WHERE `id` =100 LIMIT 1 ;
Is there a command where I can just type one line such as


Expand|Select|Wrap|Line Numbers
  1. UPDATE `mytablename` SET `price` = '3.99' WHERE `id` =26-100 LIMIT 1 ;
Do I need to use the WHERE Command?

I am new to MySQL but really want to learn it becaue manipulating it in
excel is taking a long time. Anything would help.

Thanks
Jul 15 '14 #1
12 1260
Rabbit
12,516 Recognized Expert Moderator MVP
Expand|Select|Wrap|Line Numbers
  1. `id` BETWEEN 26 AND 100
Jul 15 '14 #2
intrepid21
7 New Member
Thanks Rabbit! That worked.
Jul 15 '14 #3
intrepid21
7 New Member
Lets say I have 100 products with 'id' 1-100 that are mostly 7.99 (price) and every once in a while have a product for 14.99 in that 'id' range

I would like to change the price of the ones listed at 7.99 to 3.99 and the ones listed at 14.99 to 6.99 within that 'id' range of 1-100

I know I can use the UPDATE and BETWEEN function such as

Expand|Select|Wrap|Line Numbers
  1. UPDATE `mytablename` SET `price` = '3.99' WHERE `id` BETWEEN 1 AND 9
but if the 10th 'id' where 14.99 I would have to run another single command

Expand|Select|Wrap|Line Numbers
  1. UPDATE `mytablename` SET `price` = '6.99' WHERE `id` =10 LIMIT 1 ;
Is there a way to combine these into one single command so that it will look for all the 7.99 prices within that 'id' range and UPDATE them to 3.99 and look for all the 14.99 prices within that 'id' range and update them to 6.99

This would save me so much time!
Jul 15 '14 #4
Rabbit
12,516 Recognized Expert Moderator MVP
Expand|Select|Wrap|Line Numbers
  1. CASE `price`
  2.    WHEN 7.99 THEN 3.99
  3.    WHEN 14.99 THEN 6.99
  4.    ELSE `price`
  5. END CASE
Jul 15 '14 #5
intrepid21
7 New Member
So would it look like this

Expand|Select|Wrap|Line Numbers
  1. UPDATE `mytablename` SET `price` = '3.99' WHERE `id` BETWEEN 1 AND 100 
  2.  
  3.     CASE `price`
  4.        WHEN 7.99 THEN 3.99
  5.        WHEN 14.99 THEN 6.99
  6.        ELSE `price`
  7.     END CASE
I'm just confused where to set the range for the 'id' because I have thousands of other products within the table that I don't want to update
Jul 15 '14 #6
Rabbit
12,516 Recognized Expert Moderator MVP
Expand|Select|Wrap|Line Numbers
  1. SET `price` = CASE ...
You already set the range on id with the BETWEEN.
Jul 16 '14 #7
intrepid21
7 New Member
I am getting syntax errors with the following

Expand|Select|Wrap|Line Numbers
  1. UPDATE `mytablename` SET `price` = 
  2.  
  3. CASE `price`
  4. WHEN 7.99 THEN 3.99
  5. WHEN 14.99 THEN 6.99
  6. ELSE `price`
  7. END CASE
#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 'CASE' at line 7



Also when I try it like this

Expand|Select|Wrap|Line Numbers
  1. UPDATE `mytablename` SET `price` = 
  2.  
  3. CASE `price` WHERE `id` BETWEEN 1 AND 100
  4. WHEN 7.99 THEN 3.99
  5. WHEN 14.99 THEN 6.99
  6. ELSE `price`
  7. END CASE
#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 'WHERE `id` BETWEEN 1 AND 100
WHEN 7.99 THEN 3.99
WHEN 14.99 THEN 6.99
ELSE `p' at line 3

Any hint or solution would help...
Jul 16 '14 #8
Rabbit
12,516 Recognized Expert Moderator MVP
Please use tags when posting code or formatted data.

In your first attempt, your error code says line 7 when it looks like it means line 3. Which means that the code you posted is not the code you tried to run.

In your second attempt, for some reason you put the WHERE condition inside the CASE when I didn't do that in my code.

It looks like you are having a lot of trouble with basic SQL. Before you take on a project like this, you should probably learn the basics first.

Here is a tutorial you can use to learn the basics: http://www.w3schools.com/sql/
Jul 16 '14 #9
Rabbit
12,516 Recognized Expert Moderator MVP
It looks like in MySQL, the end of the CASE statement is just END and not END CASE.
Jul 16 '14 #10
intrepid21
7 New Member
Thanks Rabbit. I have read the tutorial and doesn't have much on using the CASE with the BETWEEN functions

The code below works great

Expand|Select|Wrap|Line Numbers
  1.  
  2.     UPDATE `mytablename` SET `price` = 
  3.  
  4.     CASE `price` 
  5.     WHEN 7.99 THEN 3.99
  6.     WHEN 14.99 THEN 6.99
  7.     ELSE `price`
  8.     END 
  9.  
  10.  
However this will update every ProductID with a price of 7.99 to 3.99 and every ProductId that is 14.99 to 6.99 which is great but I'm only trying to update a range of ProductIds from over 10,000

The BETWEEN function I used earlier would also only update a range of ProductID's with one value

Expand|Select|Wrap|Line Numbers
  1.  UPDATE "mytablename` SET `price` = '3.99' WHERE `id` BETWEEN 3000 AND 5000 
Do you have any other references on using the BETWEEN with CASE
Jul 16 '14 #11
intrepid21
7 New Member
Rabbit Thanks for your help! I'm an novice to this but it worked out.

Expand|Select|Wrap|Line Numbers
  1. UPDATE `mytable` SET `price` =    
  2.  (
  3.           CASE `price`
  4.        WHEN 3.99 THEN 1.99
  5.        WHEN 9.99 THEN 6.99
  6.        ELSE `price`
  7.     END 
  8.   )
  9. WHERE `id` BETWEEN 2000 AND 4000 
Thanks again :)
Jul 17 '14 #12
Rabbit
12,516 Recognized Expert Moderator MVP
Glad you were able to work it out. Good luck on the rest of your project.
Jul 17 '14 #13

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

Similar topics

0
by: Murphy | last post by:
I am currently migrating a db from SQL Server & MySQL and ? (front end yet to be decided upon) As I familiarise myself with MySQL populating tables etc can be quite clumbersome. After reading the...
0
by: taras.di | last post by:
Hi everyone, I've come across an extremely strange problem. The exact same query in both mysql command line client, and mysql query browser gives entirely different results. I was hoping someone...
14
by: Ben | last post by:
I don't know whether anyone can help, but I have an odd problem. I have a PSP (Spyce) script that makes many calls to populate a database. They all work without any problem except for one...
2
by: Flic | last post by:
Hi, I have a basic db that I access with MySQL query browser. Everything seems fine to me but I am using this db as part of a php shopping basket and when I try to add an item I get: Notice:...
2
by: stmfc | last post by:
i have a problem with user creation for mysql db i think the problem stems from string style. GRANT ALL ON newDB.* TO 'test'@'localhost' IDENTIFIED BY 'test'; i am...
4
by: TechnoAtif | last post by:
Hi ALL I have entered some array values using checkboxes into mysql database through a form. Next iam creating a searchpage where all those cateogories inserted through checkboxes has to be...
3
by: emsik1001 | last post by:
Hi http://dev.mysql.com/doc/query-browser/en/mysql-query-browser-connection.html I'm trying to connect from my Windows based PC to a dedicated server which is running on Linux system. I...
1
by: giovannino | last post by:
Dear all, I did a query which update a sequence number (column NR_SEQUENZA) in a table using a nice code (from Trevor !). 1) Given that I'm not a programmer I can't understand why...
5
by: lisles | last post by:
i have a page funtion.php which hs the function to connect to the db /* Mysql Connection */ function connect(){ global $db_server,$db_user,$db_pass,$db;//Global Values from the config.php...
5
by: neovantage | last post by:
Hi, I have a scenario in which i am stuck with in my custom shopping cart. I have tables of category and product.Each product is against some category. for example Category Table Fields are...
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
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...
1
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
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.