Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
 
  HOME    TRACKER    MYSQL




Update Table By ID Ranges


How do i use the UPDATE statement to update a range of Primary Key id
numbers. (let say 100 through 1000).

Example,

UPDATE testTable set testField='9999' WHERE
test_id in (100:1000);




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
3,000,000 Unique INT(11) Low-Hi Ranges In My TABLE, Am I Stuck With FULL TABLE SCAN?
I have a table with 3,000,000 different IP netranges.

each row has a unique range: ie: 1xxxxxxx-1xxxxxxx2
..

I try to find the primary_index when querying WHERE var BETWEEN lowIP and highIPrange.

Since there all of the netranges are unique, it seems like my INDEXES are useless, so the only way to optimize this would be to horizontally partition the data? The query is extremely slow ~1 second in MyISAM, and ~.50 second in MEMORY table format.

I'm assuming the only option is to horizontally position the data to improve performance, am I correct?

Date Ranges.
I have a table with two fields "startDate" and "endDate".

I want to be able to search a date range against these two fields as another date range. Can I do that? How?

So, if my records are:

number |startDate | endDate |
number1 |2006-01-31 | 2006-09-12 |
number2 |2006-02-01 | 2006-05-06 |
number3 |2006-04-01 | 2006-07-01 |
number4 |2006-01-01 | 2006-12-31 |

And I want to find any records that have any dates within my user defined range.
So,
if I query between ��-3-31' and ��-06-02' return records (number1,2,3,4).
If between ��-01-01' and ��-03-01' return records(number1,2).
If between ��-06-01' and ��-10-01' return records(number1,3,4).

Getting Available Date Ranges...
I have a table, it's called tbl_propertytaken. It's a list of propertyID and a date, basically saying a particular property (it's a real estate site) is taken for a specific day.

Now, on the front end, there's a search to determine if a particular date range is available. Basically, vacationers want to be able to return a list of properties that are available for the date range given to them.

So basically, there's a bunch of other search criteria in my query too, but the one that is killing me now, is that they went from a single date to a date range. Which makes sense, since noone is going to be looking for available property for a single date. :D

So the old SQL was something like this (got the count with this function, and the results from another function for pagination's sakes):

SELECT COUNT(DISTINCT pr.propertyID)
FROM tbl_property AS pr
LEFT JOIN tbl_propertypicture AS pp ON pr.propertyID = pp.propertyID
INNER JOIN tbl_location AS lo ON pr.locationID = lo.locationID
WHERE pr.isRemoved = 0 AND (pp.isDefault = 1 OR pp.isDefault IS NULL) AND (DATE(?) NOT IN (SELECT dateTaken FROM tbl_propertytaken WHERE propertyID = pr.propertyID)) AND pr.isActive = 1 AND pr.userActive = 1

The question mark is a parameter passed down via .NET and my provider. Now, I need to be able to take down a date range. Ideally I'd like to use a range, but I made a function to break a range into a string of dates delimited by a comma if we need to use that.

I just can't wrap my head around this. Any advice?

VARCHAR Ranges
Is it kosher to select ranges in VARCHAR data?  Here is an example of my data:

300
350
400
450
500
550
B300
B350
B400
B450
B500
B550

CODESELECT * FROM t WHERE `mydata` >= '4' AND `mydata` < '5'

Datetime Ranges
What is the right way to ask for records with a datetime field from the last X hours (from the last 24 hours or 72 hours for example?) in a query?

Integer Ranges
Usually I would do this in code but I've seen you SQL gurus do things with it that I never even imaged possible (btw where do you learn all that stuff) so I thought I'd ask if this is possible...

If I have a database full of entries with StartDate and EndDate (values as unix timestamps ie integers) is there any way in SQL to determine whether a given range of values eg user enters a new start date and end date, overlaps or is between any of the values in the database?

Ranges Of Records
I have records that i need to order and place in differect areas
Firstly I need to first 3:
SELECT * FROM table ORDER BY id LIMIT 3 (tested and works)
Then I need to pick up from there and get the next 7:
SELECT * FROM table ORDER BY id LIMIT 3,7 (untested)
Now i need to get the rest of them skipping the first 10 records. How do I do this. Also is my sql for the next 7 correct?

Ranges With Offset
I have a table with a date and an hours field. These two pieces of information represent a range of time. I also have
a start and an end time.
I want a mySQL query that wil get me all the records that have part (or all) of their range between the start and end time.
What I have now is this (for example, the query will be automaticly generated):

Code:

SELECT * FROM meterprod WHERE Start BETWEEN '2008-04-20 00:00:00' AND '2008-04-20 23:59:59';

Not much, but I have no idea where to go from here.

Determining Which Age Ranges Have People In Them
Making up another example here since it makes things clearer for everyone.

I have a people table. One of the fields in there is 'age' (as an integer). I want to output a series of links like the following:

People by age:
21-30
31-40
51-60

Each range would be a link to a listing of people in that age range. They stumbler for me is that I want to only show age ranges that have people in the table in that age range.

So above, the 41-50 range didn't show up because nobody in the people database had an age in that range.

I don't know how to do this without 10-11 queries each checking a range and counting the numrows returned. And I just know if I hardcode the last year to 150, there will suddenly be a need for a 151-160 range.

Any elegant way to do this without a loop or a dozen+ queries?

Searching Between 2 Date Ranges...
i have a database of orders and basically i want to be able to enter a start date and an end date and it grab all the ones it between. How can i do this if the date field is in the format: date("m/d/Y") ?

Detect Date Ranges
i have a table containing a list of 'dates' attached to 'events'.
I'm trying to display the dates returned for an event by combining dates that follow each other in date range.
Example. The resulting dates are
'1973-10-16','1973-10-17','1973-10-18','1973-10-19','1973-10-22'

And it would be shown as
1973-10-16 to 1973-10-19, 1973-10-22
I'm trying to do it in php but i was wondering if it would be possible in mysql?

Date Ranges In Rows
Using POstgreSQL I can write this

SELECT (timestamp '2004-01-31' + (i || ' days')::interval)
FROM integers;

to get n dates from '2004-01-31' up to the i-th day later, with 'i' being the only column of a table with n rows filled with integers from 1 to n.

Selecting Date Ranges
I have many files dating back 15 years. I want to be able to display the records by the following different cases:

- Select all from current week
- Select all from current month
- Select all from past 12 weeks
- Select all from past 12 months
- Select all from specified week
- Select all form specified month
- Select all from records (i got this one down)

All the records are in the database and the dates are in DATETIME format yyyy-mm-dd.

Getting Counts Based On Non-specified Date Ranges
I have a table which has, among other fields, a date field. I want to get a count of records where certain criteria are met for, say, three days in a row. For example:

NumWidgetsDate
11/1/2000
101/2/2000
201/3/2000
101/4/2000
151/5/2000
51/6/2000

I would like to know how many times 3 consecutive days have at least 10 widgets. In this case the answer is 1 because from 1/1/2000 to 1/3/2000 one day did not have at least 10 widgets, from 1/2/2000 to 1/4/2000 each day had at least 10 widgets, from 1/3/2000 to 1/5/2000, though all three days have at least 10 widgets, 1/3/2000 was already counted before so it should not count again, and from 1/4/2000 to 1/6/2000 one day did not have a least 10 widgets. Since 1/3/2000 was not counted before it would
otherwise qualify in the next set.

Update Column In Table A With The Count Of Another Table
Having my SQL-background in Oracle I'm looking for the MySQL equivalent for:

update tableA
set column1=(select count(*) from tableB where tableA.key= tableB.key)

cannot find anything similar in the manual, but probably overlooking
something.

Update One Table Value With Values From Another Table
I am trying to update one table value with values from another table, and I cannot get it to work. What am I doing wrong?

This is my SQL-command:
UPDATE tabel1 SET tabel1.name=tabel2.name WHERE tabel1.ID=tabel2.ID

Update Table A For Every Instance In Table B
I have learned how to select records from two tables but now, I need to update every record in table A based on keys found in table B.

I have a KEY which I can match to records in table B and table B has a field which I can match to records in table A.  I need to update table A and possibly table B.

How do I go about this?

Update Table With Info In Other Table
i have a case like this:

table table1 (key, accumulator)

table table2 (key, counter)

i want to, for each table1.key = table2.key, update accumulator with the info in counter, something like this:

update table1 set accumulator=accumulator+table2.counter where (? counter is the value related with the same key as the one in table1)

how do you write a sentence like this one?

Update Table From Temporary Table
I have an import process that creates a temporary table and then dumps a large amount of records into it. I need to update an existing table against this temporary table. So where the record exists I either want to update or replace (probably based on parameter but can be a separate query for each) it in the live table, and if it does not exist i want to add it.

The temporary table can contain a large number of records (100000-200000). How can I create a fast and efficient process for doing this? Is there a query I could write to do this or would I have to do this through through code (i'm working in a java environment). I think a query would be faster than processing that many records via jdbc.

How To Check For Date/time Ranges Within Record (check For Schedule Conflicts)
You want to check scheduling conflicts and you have a record like:

appointments(table):
apptID
beginningDate
endingDate
beginningTime
endingTime

It's easy enough to check if a time is within that record. Say you want to
check if 8:00am to 10:00am is available, you would use this:

SELECT apptID
FROM appointments
WHERE (beginningDate = '2006-01-19' OR endingDate = '2006-01-19')
AND ('08:00:00' BETWEEN beginningTime AND endingTime
OR '10:00:00' BETWEEN beginningTime AND endingTime)

BUT, what if you have an all-day appointment (8:00am to 5:00pm) and there
exists an appointment already scheduled from 10:00am to 11:00am. The above
query would not find it.

Another question is what if the appointment is more than two days. Say, it's
from Monday - Wednesday from 8am to 5pm. The above query would not
successfully catch it if you wanted to schedule an appointment on Tuesday.

(I might be able to generate a date range using PHP, don't know if that's
the best way)

Update Table With API
I'm trying to develope a getway between matlab and mysql. I would like to
write the result of matlab routine into mysql table without "UPDATE
.....SET..." statement, because I've have to write a different value for each
row and I have to write a lot of row. I would like to write table row by row
sequentially.

UPDATE Table SET
Does anyone know how to implement this into a php form CORRECTLY? I have all the proper syntax but then the server gives me a message that says the mysql version may not go along with the syntax. How do I work around this then? I need to update query strings and such and nothing will work.

Update Table With API
I'm trying to develope a getway between matlab and mysql. I would like to
write the result of matlab routine into mysql table without "UPDATE
.....SET..." statement, because I've have to write a different value for each
row and I have to write a lot of row. I would like to write table row by row
sequentially.

Does anybody know how to access to table sequentially?

Update From One Table To Another ????
I have two tables with linked fields, a one to many relationship.  It is a item sold table to work order table where there are many items sold linked to a single workorder.  I want to add up all the item taxes for the particluar workorder and instert it into the appropriate workorder.  I have tried something like this:

update set workorder.tax (sum)woitem.tax where workorder.id=woitem.linkid

Can someone show me where I am going wrong? 

Table Update
How do I relate such a way that I update a column of the main table, another secondary table's column related to the main table gets updated automatically?

UPDATE From One Table To Another
This should be so simple but I'm getting a very strange error??

I'm using MySQL version: 4.1.12-log

I've got an outdated country table that I'd like to update info from an ISO table. Pretty straight forward if you ask me.

Here's the query:
UPDATE country, isocountry SET country.un_numcode=isocountry.numcode
WHERE country.country_code=isocountry.iso;

I created a column called un_numcode and then I'd like to put the iso numcode info in my table where the country_code = iso code (same data, different field names in different tables).

It goes along really well for 38 rows out of 239 rows?? And then I start getting 127 in ALL the un_numcode fields even though that value does NOT exist in the country.numcode table!

Update Table
Im trying to update a table, but Im getting the following error...
Syntax error or access violation: 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 5


Here's my code...

<cfquery>
UPDATE printers SET name= <!---this is line 5 that the error is referring to--->
<cfif IsDefined("FORM.name") AND #FORM.name# NEQ "">
'#FORM.name#'
<cfelse>
NULL
</cfif>
WHERE printerid=#FORM.printerid#
</cfquery>

Update Table
What is the right syntax of UPDATE command, I want to use multiple (more than 5) WHERE clauses and update more than 2 fields.
UPDATE table SET UPDATE1 =’xyz’ UPDATE2 = ‘abc’ WHERE 1=‘zxc’ 2=’wer’ …………?

Update Table
I want to update a number in a column adding a 1 to the current number, however I don't want to select the table first.

Update Table
everytime it tried to update row in any table, it takes very longtime, it locks the table, then i run out of connections and mysql crashes.

2-table UPDATE
I am running the following query through PHP's mysql_query:

UPDATE hotel, hotel_brand
SET hotel.hotel_brand_id=0, hotel_brand.hotel_brand_parent_id=0
WHERE hotel.hotel_brand_id=6 AND hotel_brand.hotel_brand_parent_id=6

In actuality, it's two queries combined into 1. I am deleting a value that rows in these 2 tables reference, and want to set the values to 0.
Of course, the problem is the values aren't changing to 0.

Is splitting these up the best solution?

Update One Table
I have 2 tables one for the team standings one for the scores, this select statement will calculate win loss :

SELECT
SUM((homescore>awayscore and home=teamid)
OR (awayscore>homescore and away=teamid)
) as wins,
SUM((homescore>awayscore and away=teamid)
OR (awayscore>homescore and home=teamid)
) as losses
FROM scores, teams
order by teamID

UPDATE Table
Code:

UPDATE table SET field='select concat(field1,field2) from table'

This gives me a zero in my field
I can select concat separately without any problem

Using Import To Update Table
I'm working on a business directory site that I created using phpmydirectory. All of my data was imported into mysql from the admin area of my site.

My category table has ID, Title, Description, and Keywords fields. At the time that I imported my business listings, I left the Description and Keywords fields blank. I am now wanting to go in and add descriptions and keywords to each category. I would like to setup a simple spreadsheet and fill in the description and keywords for each category, and then use Import in PhpMyAdmin. Is this possible? If so, how would I set it up? Am I making sense? I really have no idea on how to use PhpMyAdmin. I can browse around and export when I need to, but I'm scared I'll screw something up if I try to update a table.



Update A Table Being Selected?
Is there a relatively easy way to fix this bad decision I made long ago? I created a table thusly:

CREATE TABLE `blah` (
`mod_date` date NOT NULL default &#55612;&#57200;-00-00',
`mod_time` time NOT NULL default &#3900;:00:00',
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
I added a new field to the table that looks like:

`modified` datetime NOT NULL default &#55612;&#57200;-00-00 00:00:00'
I thought I could update it with:

UPDATE blah
SET modified=(SELECT CONCAT(mod_date, ' ', mod_time) FROM contacts);
But a search in this forum showed that one can't UPDATE a table that is being SELECTed from. There was mention of a temp table (and perhaps JOIN(?)), but further searching has left me wanting.

UPDATE Based On Value In Another Table
How do I update a table to remove orphaned references to a second
table? I've deleted rows in the second table, which has a unique
auto_increment key. The first table now has references to keys that no
longer exist. I need to update the first table, setting the value to
NULL where the referenced key no longer exists. Something like:

UPDATE table1 SET table2ID = NULL WHERE table1.table2ID NOT FOUND IN
table2.ID;

The NOT FOUND IN isn't SQL, of course, but I'm not sure what should go
there.

Update Based On Other Table
I know this is how this query would be written in SQL but I think something isn't working correctly since this is mySQL.

SQL query:
UPDATE watches SET watches.totalhits = ( SELECT COUNT( id ) AS totalhits
FROM watchtracker
WHERE watches.id = watchtracker.watchid )

MySQL said:
#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 'SELECT COUNT(id) as totalhits
FROM watchtracker
WHERE watches

Update A Table With Values In Another
I have the following table

tableA
column_a
column_x
column_y
column_z

tableB
column_x
column_y
column_z

How do I update the rows of tableA that match rows of tableB (all rows
of tableB are unique)

What I want to acheive is a check against tableA.column_x with
tableB.column_x, if they match, then update the row in tableA.column_y
with the value of tableB.column_y and tableA.column_z with
tableB.column_z

Update Table Problems
I'm trying to update a table where specific values are in another table. Here is the update statement I am using:

UPDATE Route_Detail,Routes SET Route_Detail.Trip_Calc='S'
WHERE Routes.Route_ID = Route_Detail.Route_ID
AND Routes.Project_Code = 'OSUT'
AND DATE_FORMAT( Route_Detail.R_Date, '%m%Y' ) = 072006

This statement runs but does not update any rows.

This select statement returns 11 rows:
SELECT RDetail_ID
FROM Route_Detail
INNER JOIN Routes ON Routes.Route_ID = Route_Detail.Route_ID
WHERE Routes.Project_Code = 'OSUT'
AND DATE_FORMAT( Route_Detail.R_Date, '%m%Y' ) = 072006

How do I get the UPDATE statement to update those 11 rows.

I'm using MySQL 4.1.21-standard version.

Very Slow Table Update
I have two tables. One is really a subset of the other. However, they came in different data files and I would like to pull data from one and put it into the other. However, it is VERY slow!

Once the tables are setup I will only read from them and perform operations. I will never update or insert. However, I can't get things setup to that point. Code:

UPDATE Based On Value In Another Table
How do I update a table to remove orphaned references to a second
table? I've deleted rows in the second table, which has a unique
auto_increment key. The first table now has references to keys that no
longer exist. I need to update the first table, setting the value to
NULL where the referenced key no longer exists. Something like:

UPDATE table1 SET table2ID = NULL WHERE table1.table2ID NOT FOUND IN
table2.ID;

The NOT FOUND IN isn't SQL, of course, but I'm not sure what should go
there.

Fields In Table Will Not Always Update,
I'm using C#, ASP.NET and MySQL, The language is no problem (not to much) but the rest well I wonder. I have a DB table that has 5 seperate fields. A main one that
is Integer and the rest are VarChar(50). each of the 4 others are identical in setup. I can change some records and others I can't.

ID int(10),
std varchr(50),
atd varchr(50),
dtd varchr(50),
ctd varchr(50)

I update say 'atd' with new data, Date, Time and a code of 15 letters/numbers.This one takes, I change to another record and try to update ctd and it shows like it takes but when I use the Command Line it shows no updates on ctd. this is random on this also.
If I change to another record it may all work or not. Any Ideas at all?

Update Fields In Table
I am trying to update multiple records,rows,fields in a table. Below is what I am placing in the sql field in phpmyadmin. It works with one update line but not more. What am I missing here?

UPDATE Products SET TitleTag='U.S. Air Force Retired Shop On-Line for Flags of the world - US, International Flags ' Where Product_ID=28;

UPDATE Products SET TitleTag='Vietnam Veterans of America Shop On-Line for Flags of the world - US, International Flags ' Where Product_ID=29;

Update Table Values
I have a csv.txt file with contacts and their information in it. Then I also have a table in mysql with the same contacts and their various information. I need to merge these to so that if a field value in the database is NULL the value that is in the csv.txt file is inserted into the database field.

I have been looking at the mySQL site on how to do this but have not been able to produce a working solution. Below is a more detailed example of what I am looking for. Code:

UPDATE Based On Value In Another Table
How do I update a table to remove orphaned references to a second
table? I've deleted rows in the second table, which has a unique
auto_increment key.

The first table now has references to keys that no
longer exist. I need to update the first table, setting the value to
NULL where the referenced key no longer exists. Something like:

UPDATE table1 SET table2ID = NULL WHERE table1.table2ID NOT FOUND IN
table2.ID;

The NOT FOUND IN isn't SQL, of course, but I'm not sure what should go
there.

UPDATE From A Linked Table
I wanna update the table COMPANY from a linked field. The following is not
accepted. Could anybody help me with what the command should be?
Thanks a lot.


UPDATE `COMPANY`
SET `COMPANY`.Region_ID =
(
SELECT `Area`.Region_ID
FROM `AREA`
WHERE `COMPANY`.Area_ID = `Area`.Area_ID
)

Multiple-table Update With Sum()
I have the quantities from different warehouses for each product in products_quantities, and the aggregate quantity for each product in products. I want to update products with the total from products_quantities. Why does this not work?

UPDATE products p, products_quantities pq
SET p.products_quantity = SUM(pq.quantity)
GROUP BY pq.products_id

I've tried every variation I can think of, but always get a syntax error near 'GROUP BY pq.products_id'. Can I not update one table with the sum of values from another table, in one query? I'm using 4.0.26.

Update Same Table With More Than One Record
I have a forum table which I need to perform the following update

1) I have an original message which I need to change its online status into DISABLE 'D' in my "status" field as follow single command:

UPDATE forum SET status = 'D' where message_id = 1 and parent_id is NULL;

*the parent_id field is NULL because it is a parent thread...

However, it has some child threads to this message...therefore I must force the all the child thread to change the status into 'D' also. So I did a second sql query as follow:

UPDATE forum SET status = 'D' where parent_id = 1;

*parent_id = 1 means it points to the parent message_id = 1

My question is

DOES anyone know a good sql query that will combine these two queries into ONE single query?

Update Linked Table
ODBC --update on a linked table 'tblname' failed.
[Microsoft][ODBC Driver Manager] SQL data type out of range (#0)
I get this message whenever I try and update or change anything. I can't even open a table, delete a number 4, replace it with 4, and it still fails.

What causes this? I have a primary in every table (though they aren't called primary) and a timestamp in each table.


Copyright © 2005-08 www.BigResource.com, All rights reserved