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




Convert A Nested Update Statement


i have a nested update statement in my sql server as follows
UPDATE DIM_EVENT .

SET INVOICE_AMOUNT =
(SELECT SUM(INV.INVOICE_AMOUNT_TOTAL)
FROM INVOICE INV
WHERE INV.INVOICE_REFERENCE_NUMBER = DIM_EVENT.OBJECT_ID
AND INV.INVOICE_AMOUNT_TOTAL IS NOT NULL)
WHERE DIM_EVENT.EVENT_TYPE_CODE IN ('OFULN', 'OAUTH', 'CRDRL');
what is the equivalent in MySQL.




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Nested Select In Update Statement
my nested Select-Update-Statement doesn't work. I think it's because the SELECT-Statement returns an empty set.. is there a posibillity to run this statement if there is no empty set and insert NULL if it is? Or is there another error at all?

CODEUPDATE proj_user_suche SET acronym = (SELECT name FROM acronym LEFT OUTER JOIN link_acronym ON acronym.id_acronym = link_acronym.id_acronym WHERE projekte_nr="23") WHERE id_proj_user_suche="17"

Convert SELECT Statement To UPDATE
How do I convert the following SELECT statement to UPDATE statement? If I'm not mistaken, UPDATE statement could not involve more than one table.

SELECT * FROM F0116 LEFT JOIN F0101 ON F0116.ALAN8=F0101.ABAN8 WHERE F0116.ALCTR='' AND F0101.ABMCU='1'

Nested SQL Statement
I'm new to MySQL and I use this frequently with SQL Server 2000. I get an error and want to know if there is something equivalent with MySQL? Code:

Nested While Loop In An SQL Statement
Hope the title isn't too off putting!  

Anyway I am having trouble getting the following to display what I want correctly. Here's the code then I'll explain;


CODE$sql = "SELECT board FROM leaderboard";
$result = mysql_query($sql);
  while($board = mysql_fetch_array($result, MYSQL_NUM))
       {
        $query = "SELECT $board[0].rank, users.username, $board[0].W FROM users, $board[0] WHERE users.id = $board[0].id ORDER BY W DESC LIMIT 50";
         $result2 = @mysql_query ($query);
         while ($row = mysql_fetch_array($result2, MYSQL_NUM))
{echo "<tr><td align="center">$row[0]</td><td align="center">$row[1]</td>
                         <td align="center">$row[2]</td><td align="center">$board[0]</td></tr>";
           }
           }
           echo '</table>';

Nested Select Statement Error
the mysql that is loaded on the host i am on is mySQL 4.0.24 whereas the one i am using is mySQL 5.0.27 on my computer.

i am having some weird problems where nested statements like this are not working on the server i am on...
select * from table1 where id = (select id from table2 where user = 1);

this sql statement works fine on my computer but not on the server. it says there is a syntax error when it is run on the server. but this sql doesn't look wrong to me, or it does?

i have a feeling that sub queries is not implemented in mysql 4.0 and earlier....but i really don't wish to change all the sql in my scripts to the old method where i have to select * from table1, table2 where table1.id = table2.user; because it will be quite troublesome...

Testing For An Empty Set Returned By A Nested Select Statement
For those of you following the "While Loop Within Select" thread... I've been restructuring my database to incorporate the changes. Now I've got a table of skipped stops rather than a table of stops (I need it that way for various reasons) and I need to return the schedule to the user as long as the user's stop isn't in the list of skipped stops. SO say something like:

SELECT
yada yada yada
FROM
schedule AS 's1'
WHERE
(
SELECT
skippedstop_id
FROM
skippedstops
WHERE
schedule_id=s1.schedule_id
AND
location_id=$origin
) == "EMPTY SET"

Only I have no idea how to actually test for the empty set from the nested select...

Nested Select Statement Taking Hours To Execute
I am running a sub select statment so I can compare a couple of tables. If I run the statement from two test tables of 1000 records in each, it will take nine seconds to get the results that I need.

However, if I take the same statement and point it to the two tables which contain all the data I need to run against (50560 and 161680 records respectively), it takes hours to run...how long I'm not sure. I fired the statement off yesterday afternoon @ 3pm and it is currently 11:30am the following day...the select statement is still executing.

Does anyone have performance on something like this?

SHOW TABLE STATUS & The Nested SELECT Statement
The "SHOW TABLE STATUS" command seems to return a result set, just
like "SELECT * FROM SomeTable".

Therefore, since the following is valid:

SELECT * FROM (SELECT * FROM SomeTable)

I inferred that this functionality would extend to the SHOW statement:

SELECT * FROM (SHOW TABLE STATUS FROM SomeDataBase)

But such a command gives an invalid syntax error.

I want to query the data_length from a few tables, place those values
into temp variables and then run a second SQL command using those
values. Is what I'm trying to do possible?

SHOW TABLE STATUS & The Nested SELECT Statement
The "SHOW TABLE STATUS" command seems to return a result set, just
like "SELECT * FROM SomeTable".

Therefore, since the following is valid:

SELECT * FROM (SELECT * FROM SomeTable)

I inferred that this functionality would extend to the SHOW statement:

SELECT * FROM (SHOW TABLE STATUS FROM SomeDataBase)

But such a command gives an invalid syntax error.

I want to query the data_length from a few tables, place those values
into temp variables and then run a second SQL command using those
values. Is what I'm trying to do possible?

Convert UPDATE Query To INSERT
I have an SQL dump file of a sql database that was generated with the UPDATE export type. The file contains code for generating the various tables inside the database along with the UPDATE lines for the table data.

Is there a way to process the dump file such that all of the UPDATE instructions are converted to INSERT instructions? I have to restore the database from scratch so there are no existing table rows to update.

Nested Sets And Filtering Conversion To Nested Arrays, How?
I am currently working on a project where we want to reuse a tree structure stored in the database. The tree are basically groups/categories of all the content available within the project. Now we want to reuse this data in forum and only show the featured items/nodes.

An idea of the tree structure is:

+ root
+ Music (f)
+ Genre
- Electronic (f)
- Classical (f)
Now the problem is that we don't wan to show the whole tree structure within this new forum. But only the ones for example, which are flagged to be featured. For this, we have a field in the groups table. Only I happen to have some problems with reusing this tree hierarchy:

The main problem I have is that when I get the whole tree structure using the query SELECT * groups ORDER BY left_id then I have the whole tree.

Only when I add "WHERE featured=1" then the left and right node values are becoming invalid because I filtered out possible child or parent nodes.

I am curious if anyone could help me how to get the structure in such way I can easily reuse it. I would love to have the featured nodes in a nested array. Something like:

root
children: music
children: electronic, classical etc.

Select Statement Question (nested Select?)
I have a DB containing 3 fields fullname, inext, and outext. I need to see all the records that have a duplicate entry in inext. I know I can do a distinct query on the inext column but that only gives me the unique ones I need all the different duplicate records.

What Would This Update Statement Be?
I have an array of ids formatted like 1,2,3,4,5

I have a table where there is a field that selects maybe a few of those ids, and is formatted like this: 1,3,5

Update Statement
I am using excel to update an online mysql table. I am having an issue with my update statement and I don't understand why. When I run the statement, I am getting a run time error. In the description of the error, the statement is reading as I would expect it to - but it is not working.

usqlstring = "(UPDATE company set company = '" & ucompany & "' WHERE prospect_id='" & updateid & "')"

Update With If Statement
I'm trying a statement which I cannot figure out what I'm doing wrong on. I've tried every combination that I can think of and stared myself blue with no success.

update cdr set Cost=if(mod(BillSec,6)=0,int(BillSec/6),int(BillSec/6)+1)

The idea is to update Cost with a value depending on the result of a formula. It's an empty field which gets a computed value from other fields. cdr is the table.

Update Statement
here is a update statement I have...

UPDATE support_test
    SET cat_id=42
    WHERE cat_id = 4

It works fine, only thing is I need to update more numbers but I don't want to overwrite the updated numbers.  Example, I want to update 42 to 6 but want to update the original 6's to 12. I have about 30+ numbers that need to be changed, any way to go about this?

SQL-Update-Statement
I have to update all UA-fields in the table idrz_kennzahl with primary
key idrz, where the same idrz in the table per_verwaltung with the
field Amt=12 is.

I designed following statement but it doesn't work:

update idrz_kennzahl
set UA=1234
where
idrz_kennzahl.idrz =
(SELECT per_verwaltung.idrz from per_verwaltung where
per_verwaltung.Amt=12

Update Statement
I am currently updating a table and increasing a column value by 1, the problem is that he don't increase 1 but it does increase the double of the value specified.
Why does it performs the update like this? And how can i solve this problem?

SQL Statement Syntax used: UPDATE table_name SET column_name=column_name+1 WHERE id=1

Version of MySQL: 4.1.7 for Windows installed in Windows 2003 Server.

Update Statement
i have the following problem. in a table i have 3 columns. first a nvarchar field (6 characters) which is filled with point of sales (pos) numbers. the second one is an ip-adress like 10.192.17.30. the third column i want to create based on the second column, means i have to substract e.g. 3 of the ip-number. e.g. the ip-numer is 10.192.17.30 so in the third column should stand 10.192.17.27.

Update Statement
I have two tables, and I want to take data from one table, and overwrite it onto the second. Unfortunately, my SQL is pretty weak.

The first table has a mediumtext column and a id (primary key) column. The second has an id (the same as the first), a number, and a mediumtext column.

I want to take the contents of the mediumtext field with the highest number and replace the contents of the field in the first column with it with the same id.

My instinct is to do it this way:

update forum1post p
set p.pagetext = (select query1.message
from (select pe.message, max(pe.editnum)
from forum1post_edits pe
group by p.postid) query1)
where p.pagetext = 'Why did you all flame my topic. Screw your ****ing forums.'
and p.postid = pe.postid

However, that does not work, as pe is not recognized.

SQL-Update-Statement
I have a litte problem with my sql-database!
I have to update all UA-fields in the table idrz_kennzahl with primary
key idrz, where the same idrz in the table per_verwaltung with the
field Amt=12 is.

I designed following statement but it doesn't work:

update idrz_kennzahl
set UA=1234
where
idrz_kennzahl.idrz =
(SELECT per_verwaltung.idrz from per_verwaltung where
per_verwaltung.Amt=12).

Update Statement
UPDATE `zipLocations` SET `z_zipCode` = '06001' WHERE `z_zipCode` =6001 LIMIT 1 ;

Field Type Attributes Null Default Extra Action
z_zipCode int(5) No 0
z_cityName varchar(50) No
s_stateAbbr char(2) No
z_countyName varchar(50) No
z_areaCodes varchar(50) No

I even went through phpmyadmin found the row clicked on the edit button for that row and updated it. and it still doesn't update.. im' thinking it has to do with the data type being INT...

Why doesn't it update it ?.. It says it was successful.
Affected rows: 0 (Query took 0.0002 sec)

Update Statement
UPDATE products_to_categories SET products_to_categories.categories_id = 666
FROM products, products_to_categories
WHERE products_to_categories.products_id = products.products_id
AND products.products_model = 'PAC-P6512'

Update Statement
Can someone please let me know what is wrong with the following statement?

UPDATE user_attribute, user
SET user_attribute.value = user.login
WHERE user_attribute.userid = user.id;

I keep getting a syntax error but don't see the mistake made.

Update Statement
this works on my machine but not where my site is hosted. They do use an older version of mysql could that be problem

update inv, newprice set inv.cost = newprice.price where inv.itemcode = newprice.itemcode

works fine on my machine her ebut on server i get error..

Conditional Update Statement
I have a table with a field of ID numbers. I want to update this field and replace the ID numbers. For example I want every record that currently has the ID number 2,5,6,or 9 to be replaced with a 1. Every record with 1 or 4 to be replaced with 2 ...

I can't seem to form the update statement to do this. I can do them one at a time but this won't work because if I change 2,5,6, and 9 to 1 then when I run then update for 1 and 4 it will change them all. Does this make any sense?

I want some way to be able to use a condition...if id=2 or 4 or 6 set it to 1 and if id= 1 or 4 set it to 2.

My failed attempt:

mysql> update client_software set software_id=1
WHERE (software_id=2 or software_id=5 or software_id=6 or software_id=9)
AND software_id=2 WHERE (software_id=3 or software_id=7 or software_id=8)
AND software_id=3 WHERE (software_id=1 or software_id=4);

DELETE,UPDATE Or Other Statement In WHERE
I would like to let my admins write WHERE statements. . In panel where you can see all users of my site, admin should have input where he can put some filters in WHERE statment. E.G. standard query is
SELECT login FROM USERS .
and admin would write age > 18 and male = 'f'
. php script would conact it and it would execute
SELECT login FROM users WHERE age > 18 and male = 'f'
I have question about security of this solution. Is possible to put in WHERE statement sobe dangeurous statemetns like DELETE of UPDATE?

Update Statement Problem? Plz Help.
I've a simple doubt of whether can or can't i update two tables by using joins as below. the below update statement matched 2 rows one each for two tables but didn't update.

mysql> update audio join pcr_info on audio.audio_sid=pcr_info.pcr_sid set audio.
audio_pid=4896,audio.audio_prgm_num=3,audio.audio_type="MPEGLayer1",audio.audio_
sr=44.1,audio.audio_mode="1_0",audio.audio_bitrate=320,pcr_info.pcr_pid=49 where
pcr_info.pcr_prgm_num=3 and pcr_info.pcr_sid="2501" and audio.audio_pid=4896 an
d pcr_info.pcr_pid=49;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 2 Changed: 0 Warnings: 0

Very Slow Update Statement
I am having an issue with an UPDATE statement that takes a very long time. I am using
1 table in a schema to update another table in another schema. Below are the create statements and the update statment I am using. Table and column names have been changed to protect the innocent :) Code:

Special UPDATE Statement
UPDATE some_table st SET some_page_no=((select count(*) FROM some_table WHERE some_table_id<st.some_table_id and some_id=st.some_id and some_type='OK') div 10)+1 WHERE some_id=345 and some_type='OK'

I want to use the above to make a fast page index (10 on each page). I had something similar in mssql and it worked, but in myssql I get this error:

"You can't specify target table 'some_table' for update in FROM clause"

What have I done wrong or can the above simply not be done?

Select + Update In One Statement
I have these statements.

$selectdata = "SELECT * FROM imagetrack WHERE fimsid = ".$_GET["fimsid"]." AND name = '".$_GET["service"] . "'";

$query = mysql_query($selectdata) or die(mysql_error());
$update = "UPDATE imagetrack SET seen = seen+1 WHERE fimsid = ".$_GET["fimsid"]." AND name = '".$_GET["service"] . "'";

Basically i was wondering if there was a statement where i can select everything and update the seen in one statement.

Conditions In UPDATE Statement
i have some data to be updated in a specific row - WHERE id=1

data:
$min_time, $max_time, $time, $attempts

so, i would like to calculate new avg and replace min_time with new value, if new min_time is smaller then that one stored in the database.

"UPDATE mytable SET
avg=(avg*attempts+$time) DEV (attempts+1),
min_time=**here i could use help** ??IF(min_time>$min_time)$min_time ELSE min_time??
**and similar with max_time .

Select And Update Statement
Is it possible to combine a select and update statement in mysql? If
so, what is the syntax.

SQL Statement For A Selective Update
I have a table with a field called 'imagepath', in this table is data represented by the two following path types:
1. J:DIRDIRDIR
and
2. 172.21.2.7DIRDIRDIR
What I need to do is udpdate the data to represent the following:
172.21.10.1DIRetc.

In other words don't update the dir paths just update the mapped dirve and old unc to the new unc.

UPDATE Statement Doesn't Work
TABLE - abc

ID NAME
___________
1 hello
12 abc


UPDATE `abc` SET `ID`='5' AND `NAME`='hello' WHERE `ID`='1' AND `NAME`='hello'

wherenver I try to update this table using above sql statement, it doesn't work. I just get "Row match:1 Changed:0" message.

If Condition In Update Statement
I have a table with three fields - stockLevel, backOorder and allocated (there are other fields but these are the inportant ones here).

Is it possible to create a SQL update query that will deduct a qty from the stockLevel if there is sufficient stock and if not deduct as much as possible and put the this amount on allocated and the remainder on backOrder?

Long Old UPDATE Statement
I have the following SQL which gives me the row Im after

SELECT *
FROM staff, staffemails
WHERE staff.users_username='$user'
and staff.staff_id = staffemails.staff_id

thing is I now want to perform an update on the result of this query. Ive tried

UPDATE staffemails
SET is_registered ='0'
WHERE staff.users_username = '$user'
and staffemails.staff_id = staff.staff_id .

UPDATE / JOIN STATEMENT
i have problems after updating mysql from 4.x to 5.0.19. This statement should update about 3000 rows but it only does 15 but there are definitely another 2000 rows with an older date and matching nr!

UPDATE tab1,tab2
SET tab2.newdate = NOW()
WHERE tab1.nr=tab2.nr;

I tried the variation with inner joining tab1 as well. same result.
Does anyone has an idea solving this problem? Perhaps it´s a bug?

Update Statement Not Working
Why does this simple update statement not work with a mysql version 3.23.49,
it does work when using 4.0.24..... ?

UPDATE listitem,vjbase,list SET listitem.showsec = 7 WHERE listitem.id_vjbase = vjbase.vjbase_id AND listitem.id_vjbase = 3 AND vjbase.id_viduser = 61000 AND listitem.id_list = list.list_id AND listitem.id_list = 1 AND list.id_viduser = 61000

MySQL/PHP - SQL UPDATE Statement
I am trying to update one field in my table in MySQL.  I cannot get this field to update.  Can someone possibly tell me what is wrong with my SQL statement?

$SQL="UPDATE umcritsurv SET IQ_Use_Q9='$IQ_Use_Q9' WHERE fldAuto='54'";

Help With Simple SQL Statement To Update Zipcodes
I have a varchar(5) field in a database called 'zip_code'. I imported some zipcodes from an Excel spreadsheet, but the spreadsheet had truncated any zipcode that started with a "0" and removed the zero, so the correct zipcode of "08993" was incorrectly placed into the record as "8993".

Would someone help me write a SQL statement to run on the field zip_code that would look at the data, if it was four characters long, add a "0" to the start and keep the remaining four characters in place?

QUERY One Table And Update Another In ONE Statement?
ZIP_CODE:
zip_code
latitude
longitude

customers:
customer_id
...
zip_code
latitude
longitude

The lat and long in the customers table is blank. I want to lookup customers' zip code in the ZIP_CODE, get the related lat & long and update the customers table with that information. The result being that after this query is run, the customers table now has lat & long data.

UPDATE Statement To A Single Position
I am running into problems trying to write SQL to update a single position within a field for throusands of records.

Thousands of records on a table have an incorrect number in a field (UIDY) and I am looking for the most simple way to update them all.

If it were only a single record, I would execute the following:
Update MF_VEND_ADDR_DOC set UIDY = '&4027&7608&7607&1609&VC19&VC19000055&&0&999&M' where UIDY = '&4025&7608&7607&1609&VC19&VC19000055&&0&999&M'

The only difference in those two lines is that I want to change the &4025& to &4027&.  

Does anyone know of a way to update thousands of similar records by updating the 5th position in the UIDY field, thus eliminating the need to list out each UIDY value?

CASE Statement In Update Command
I'm trying to update one table (tbl_usage) based on information from another table (tbl_subscriptions) joined by the userID. The usage table tracks the number of times a user hits a page, and decrements the count each time. When the user has a zero count, they are redirected to a warning page.

Each week the count is reset depending on what type of license they have (licenseType) and how many of those licenses they've bought (numLicenses). A group license (G) gives them 100 hits, and an individual license (I) gives them 5 hits.

I'm trying to use a case statement to perform this count reset operation, but I can't seem to get it to work. The following case statement works just fine: Code:

MySql Update Or Insert Statement To Modify A Word In All Caps
I have a field in my database called Subtopic. In this field I have a word LITERATURE in all caps. I need to change the word LITERATURE to LITERATURE followed by a colon then an empty space. Can someone give me the MySql statement I can paste into phpMyAdmin to make this change? Note: The Subtopic field contains the word "literature" but not in all caps because the Subtopic field is part the database that is an encyclopedia. Therefore, I want to change ONLY the word LITERATURE if the word LITERATURE is in all caps.

Nested Sql
if nested select statement will work in mysql 3.23.53? i have a simple nested select statement which i believe no error on it.
here is my nested sql
SELECT
picid
FROM
picture
WHERE picid IN (SELECT picid FROM lightbox_image WHERE lbid =1)

Nested SELECTS?
SELECT PPosts.* , (SELECT Count(ID) FROM PComments WHERE PComments.ForPostID = PPosts.ID) AS CommentCounts FROM PPosts

Well is this possible in MySQL 4? ? ??

Cos i've developed my site in MySQL5 and it worked fine with this, but when i put on the hosting server which is MySQL4 i get this message:

#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) FROM PComments WHERE PComments.ForPostID = PPo

Nested SQL Query?
I'm trying to write a query that picks DISTINCT contest entries (based on distinct email addresses), but also randomizes the selection. I have the following now, but isn't correct because it picks a few duplicates.

SELECT *
FROM contest_entries
submitted_date BETWEEN 'datestart' AND 'dateend'
GROUP BY email_address
ORDER BY RAND() LIMIT $maxWin

* I just need display email_address, first_name, last_name, but I've been selecting all for some reason.

Nested Select
is it posibile to select like this:

SELECT *,(SELECT name FROM tablex WHERE cid=table.id) as country FROM table
i would like to give the result from first select as condition for inner select

so i get table.id from first select query wich will be condition for result of inner select

anyway i would like to get result in one row ..like *,country



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