INSERT Into New Table, SELECT From Old Table, UPDATE Old Table With New Key
Code:
INSERT INTO retailers
(retailername, retailerdesc, retailerwebsite, retailerurl, active)
SELECT
datasource_retailername,
datasource_retailerdesc,
datasource_retailerwebsite,
datasource_retailertrackurl,
1
FROM
datasources_retailers_idx AS i
LEFT OUTER JOIN
retailers AS r
ON
r.retailername NOT REGEXP REPLACE(i.datasource_retailername, ' ','.+')
UPDATE datasources_retailers_idx SET idretailers=last_insert_id();
Here's what I'm trying to do:
* Select from old table
* check if there is a matching retailer in the new table
* If not, insert retailers details into new table
* Update a reference column in the old table with the INSERTID primary key value of that row from the new table
View Complete Forum Thread with Replies
Related Forum Messages:
INSERT / UPDATE A Row By Its Position In The Table?
Is there a way to insert or do an update of a row (aka record) by its position in the table, for example, the 5th row of the table? I know you can select data in such a manner, using the LIMIT 4,1 clause, for example. Does this work with insert/update as well, and if so, what's an example SQL syntax?
View Replies !
Insert And Update Entire Table
My website will modify records on my products table. But I need to occasionally update their qty and their prices without losing any changes already made by other users online (like qty sold). I'm able to do that now. It works really well:
View Replies !
INSERT INTO TABLE... ON DUPLICATE UPDATE.. Query
I have seen in mysql site, there is one query for inserting and updating record. So i have made that query for my table like below INSERT INTO contacts( id, name, email ) VALUES ( '?', 'XYZ', 'xyz@yahoo.com' ) ON DUPLICATE KEY UPDATE name = 'xyz', email = 'xyz@yahoo.com' Here in this case for id i have used "?", because every time i m not checking what is the last id. But it is not working.. It is giving me error. #1366 - Incorrect integer value: '?' for column 'id' at row 1
View Replies !
Select From Table Before Insert
I am trying to create a trigger that will select the value for an integer from the table on which the insert is occuring, increment it by one, and insert the value into the new row on the same table. Basically, we have a user referral id. I want to grab the rank of the referral user, increment it, and insert it into the rank of the new user. i cannot figure out how to do this for the life of me. Code:
View Replies !
Insert Into Table A Select
I am testing out the innodb table types and I think that this statement should be able to work according to the transaction model for Innodb tables. create table a (c1 char(10) type=innodb; insert into a values ('aaa'); commit; insert into a select * from a; I get the following error: ERROR 1066: Not unique table/alias: 'a'
View Replies !
Select Data From One Table Insert To Other
Can anyone give me some hints about a query. A query which takes a value from form field insert it to 1st table, then select other table within that query to select name,email from 2nd table and insert it to the 1st table from 2nd table,but condition is that in where clause it must check that dcode entered in the form value must match with 2nd table dcode(dcode is field in table.).
View Replies !
Insert Into Table With A Select Statement
$query = "INSERT INTO navigate (user_id, session_name, number_of_times, start_time) select user_id from username where username = '$the_session', $the_session,0,'$now')"; Is this the correct syntax? if not please show If you can not do it this way then how else could you do this? These are the four columns I am inserting into navigate table. They are 1. user_id 2. session_name 3. number_of_times 4. start_time I have four values that go with the four columns. They are 1. (select user_id from username where username = '$the_session') 2. $the_session 3. 0 4.'$now'
View Replies !
1 Simply Query Problem. Please Help With Insert Into 1 Table From Another Table
I have Table A that has some records already in and then I have Table B that does a few things and gets updated regularly. With Table A, I want to get it updated every 8 hours or so with new data that has been inserted into Table B. I will do this using the Cron. But what would be the best insert query to use so that it does the process really quick. The query that I have so far is: Quote: mysql_query("INSERT INTO table a (id, title, descrip) SELECT id, title, descrip FROM table b where app=1"); With the above query the id's match in both tables. But I only want the new records to be inserted into Table A where app=1 in Table B and the row is not already in Table A. How can I add to the query so that it does this.
View Replies !
Table Design Question? House Table, Owner Table, Code Violations Table - Best Way?
Given the tables: HOUSE house_ID address OWNER owner_ID name telephone... HOUSE_OWNER_JOIN ? CODE_VIOLATION_HISTORY house_ID violation_ID violationStatement ... My goal is to be able to track code violations of the house PER owner. For example, I need to display a page that shows the current house with it's coe violations and a link to show the HOUSE's history of violation regardless of owner, Like: House 1009283 Address Past history (link to the following) House History 2001-01-04 Owner: John Smith Code Violation: Gutter issue 1999-06-01 Owner: John Smith Code Violation: Faulty Steps 1998-03-02 Owner: Sam Spade Code Violation: Driveway carcks 1990-01-12 Owner: Keith Sledge Code Violation: Grass untidy For the design of the HOUSE_OWNER_JOIN table, I thought of two ways I could go on this and this is where I need your help. Option 1: Have the HOUSE_OWNER_JOIN table keep dates so I can track the ownership changes that way: HOUSE_OWNER_JOIN houseID ownerID dateOwnershipBegan dateOwnershipEnded then I could look up all code violations by date and associate them with their rightful owner. ================================================== Option 2: Have the HOUSE_OWNER_JOIN table be the primary keeper of identity data by adding a new primary key and changing the CODE_VIOLATION_HISTORY table to reference that table by chaning the referencing key from house_ID to house_owner_ID: HOUSE_OWNER_JOIN house_owner_ID houseID ownerID dateOwnershipBegan dateOwnershipEnded CODE_VIOLATION_HISTORY house_owner_ID violationStatement ...
View Replies !
CREATE TABLE, INSERT INTO With SELECT In Parentheses
Assuming a legal SELECT statement, this works fine: CREATE TABLE Foo SELECT ... but this does not: CREATE TABLE Foo (SELECT ...) This is a problem for me as I'd like to use the output of a SELECT...UNION...ORDER BY statement as input to CREATE TABLE. Same holds for INSERT INTO. Is this a bug?
View Replies !
Update Table Based On Email In Another Table
I'm having trouble updating the entries from a table. The situation is as follows: Customer table contains: 1) customer_email_address 2) customer_newsletter (value 0 or 1) Visitor table contains: 1) email The visitor table contains email addresses from customers that have signed up through another system. I would like to update the customer table and set customer_newsletter to 1 where customer_email_address matches email from the visitor table.
View Replies !
Trying Abort One Select Accessing One Table Locked By Lock Table.
I'd like to configure one time to abort one transaction that are accessing one table locked by lock table write command ex: Transaction A mysql> start transaction; mysql> lock table mytable write; After I'll start new transaction B (another connection) mysql> start transaction; mysql> select * from mytable; It's will become long time to wait... My transaction B never "die". I tried to configure innodb_lock_wait_timeout=20 in my.ini without success,
View Replies !
Rights To Create Table, Select, Then Drop Table..
I have a need to get data from the db that requires me to=20 1) do a select and create a new table with the results=20 2) run a query against that new table=20 3) drop the new table=20 I have a script on my server that does this using the root account that has all on *.* for the db. It works fine.=20 I now want to get these results on a web page.=20 I want to create a new db user for my .php web page to use to connect to the db that only has the needed priviledges on that specific db to get the job done.=20 what priviledges do I need to give that user?=20 currently I have the following but the user can't even log into the db from the command line..=20 mysql> show grants for user;=20 +----------------------------------------------------------------------- ---------+=20 | Grants for user@% |=20 +----------------------------------------------------------------------- ---------+=20 | GRANT USAGE ON *.* TO 'user'@'%' IDENTIFIED BY PASSWORD '6fe4c0ab2cf30ae3' |=20 | GRANT SELECT, INSERT, UPDATE, CREATE, DROP ON `db1`.* TO 'user'@'%' |=20 +----------------------------------------------------------------------- ---------+=20 2 rows in set (0.00 sec)=20 when I do a "show grants for user", what should I see to allow what I want?
View Replies !
Select Count Of Data Appearing In One Table From Another Table
I have three tables: t_Products (id, name) t_Shop (id, location, name) t_Carries (product, shop) If Shop carries a product, there will be a value pair in t_Carries but otherwise no record is listed. Is it therefore possible to return a list in MySQL showing something like this shop.Id, product.Id, count(or something) 1 1 0 1 2 1 1 3 1 1 4 0 Or must I use two query and programmatically generate the list?
View Replies !
Select Data From 1 Table Based On Criteria From Another Table
is it possible to select all the data in one table based on a criteria from another table? for instance i want to select all the therapist from massage_therapist WHERE massage_schedule.finish > 0. i don't want merged results. i just need to list all therapist based on the where criteria from a different table. these two tables have the therapist_id in common.
View Replies !
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?
View Replies !
Update A Table With Data From Another Table
I have two tables with similar data. The firs table contains data that is to be updated with data from the second table. The first table (tblA) has a unique key, but the second table (tblB) does not. I have to use the 'lastname', 'firstname' and 'dept' fields that are in both tables and join the tables on those three fields. I have tried: update tblA, tblB set tblA.empPty=tblB.empPty where ((tblA.empLName=tblB.empLName) and (tblA.empFName=tblB.empFName) and (tblA.empDept=tblB.empDept)); with some test data where I know I have a match using the three fields, but nothing gets updated.
View Replies !
Select From Table Where Not Exists (huge Table)
Code: SELECT DISTINCT(`DestinationName`), ArrivalAirportCode FROM `offers` WHERE ( (`DestinationName` NOT IN (SELECT `alt_name` FROM `resort_alt`)) AND (`DestinationName` NOT IN (SELECT `resort_name` FROM `resort_resorts`)) ) AND `DestinationName` != "" ORDER BY `DestinationName` ASC
View Replies !
Select All Records From One Table That Does Not Appear In Another Table
I need to select all records from tableA that does not appear in tableB. I am using the following query that does work but is very very slow. Is there anything i can do to speed up the query? select tableA.ID, tableA.Name, tableA.Surname from tableA where tableA.ID != ALL (select tableB.ID FROM tableB where tableB.status = 'inserted' or tableB.status= 'edited' or tableB.status = 'deleted' ); I have as example 6000 records in tableA and 2000 records in tableB. tableB is used to track which records have been inserted, edited or deleted from another system so the query should return all records that do not exist in the other system yet. This query runs for about 3mins and just gets slower the more data there is. Can anyone suggest anything that could make this a sub second response?
View Replies !
Two Table Query: Grab Rows From One Table Even If No Related Row In Other Table
PHP $gettray = mysql_query("SELECT trailers.title, trailers.link, trailers.movie, movie.title AS mtitle FROM trailers,movie WHERE trailers.movie=movie.word ORDER BY trailerid DESC LIMIT 6",$connm); It works great, but there is one problem. It will not grab any rows from the 'trailers' table if a corresponding movie row does not exist in the 'movies' table. I want it to pull ALL rows from the 'trailers' table, even if the corresponding row in the 'movies' table does not exist yet. If the row does not exist in 'movies', the program than uses the entire trailer title like so PHP if($ttray['mtitle']) { $newttitle = explode("-",$ttray['title']); $newttitle = array_reverse($newttitle); $ttitle = $newttitle[0]; $ttitle = $ttray['mtitle'] ."- ". $ttitle; } else { $ttitle = $ttray['title']; } Thanks Ryan
View Replies !
Update Table From Another Table
I have 2 tables: country and ticket country table contains countryId and countries ticket table contains many fields, and a country field the country table is new and consists of all countries to be used in a drop down on the ui, that is joined with the ticket table to display the correct country based on the id. that being said.. the current ticket table also contains a country field which will be eliminated in the future. what I want to do is update the ticket table by finding the closest match to ticket.country in the countries.country table and then update the ticket.countryId to the countries.countryId. This is what i have been doing country by country: Expand|Select|Wrap|Line Numbers
View Replies !
Insert Into Different Table
I have two tables and i want join the two of the primary id's in to one table. the database is mysql. ex. Table Item (ID int(10) NOT NULL auto_increment) Table Actor(ID int(10) NOT NULL auto_increment) to go in to Table actor_item(actor_id, item_id) is it select then insert or what
View Replies !
Insert Into $table
function write_comment($name, $text) { $sql = "insert into 16(name,text) values('$name','$text')"; this doesnt work but if i change 16 to something with letters instead of numbers it works... how can i make it insert into a table with a name only in numbers!?
View Replies !
INSERT INTO TABLE
I have a table named A which has all the columns of table B plus some other columns, I want to insert data in to A from B however during this update I also want to set the values for the columns in A that are not in B how can I write the SQL syntax for the same? I tried this but this wont work obviously INSERT INTO A( CATEGORY, GROUPID, GROUPTITLE, ALBUMID, ALBUMTITLE, DEFAULTTHUMB, TFILE, BFILE, TITLE, DESCR, PIC_LOCK ) VALUES ( 'Nature', 'BUF', 'BUFFALO', 'LETCHWORTH', 'Letchworth Park, NY', '', ( SELECT TFILE, BFILE, TITLE, DESCR, PIC_LOCK FROM B
View Replies !
INSERT To TABLE
i am having a horrible time getting my script to accept variables please help $sql = 'INSERT INTO `table` (`first`, `last`, `phone`) VALUES ("$first", "$last", "$phone")'; all it does is put these actual terms in for values instead of accepting the data that comes from.
View Replies !
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.
View Replies !
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?
View Replies !
Update Table From TXT/CSV
I have a table With Field1(INT,8,PK), Field2(INT,6,PK), Field3(VARCHAR,255) and Field4(VARCHAR,255). I have to update records from a FIXED LENGTH TXT that contains: Field1(8 digits)Field2(8 digits)Field4(1-255 digits) or the same in CSV....
View Replies !
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.
View Replies !
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!
View Replies !
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
View Replies !
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?
View Replies !
INSERT ID From Foreign Key Table
I have a form used to populate 2 tables: Provider and Contacts. Provider has a foriegn key column - Contact1ID - which has a foreign key relationship with the prmary key - ID - in the Contacts table. The form submits data to Contacts then Provider but I need the ID generated by the Contacts submission to be entered into the Contact1ID column in the Provider table. If I use this: INSERT INTO Provider(Contact1ID) SELECT ID FROM Contacts WHERE FirstName='$contact1{FirstName}' AND Surname='$contact1{Surname}' .... all I get is a new row. UPDATE doesn't seem to allow SELECT to grab the new ID either. Any ideas?
View Replies !
How To Insert Into The Middle Of A Table
I've been trying to insert at new row into a certian point into a table without look. what I'm looking for is actually the opposite of "SELECT something FROM mytable WHERE id=some_number" .... I wanna do it the other way around so I can search a table for a certion number in the id-field and insert a new rowbeneath that... the id-numbers stay the same, no change is needed there.. any know how to do this? a short a method as possible as I'm trying to make script as fast as possible.
View Replies !
|