i got the following mysql query..I have tried many different format but cant seem to get this to work.
I got two tables. table mic.temp has three columns while table products has quite a few.
I need to update values into table products from table mic.temp. The matching column is model number.
i have written the following query but it updates all the field.I only need to update the values found in temp table and also auto increment the product table.if a value is not found then insert it.I don't mind if non existent values in temp table are entered as null.
mysql_query('INSERT INTO products(products_id, products_quantity, products_model, products_ean, products_image, products_price, products_date_added, products_last_modified, products_date_available, products_weight, products_status, products_tax_class_id, manufacturers_id, products_ordered, products_last_import, icecat_prodid, vendors_id, products_availability)
SELECT model, stock, price
FROM mic_temp
ON DUPLICATE KEY UPDATE set
products.products_quantity = mic_temp.stock,
products.products_price= mic_temp.price');
Check this:
Update products join mic_temp on products.modelnumber=mic_temp.modelnumber set
products.product_quantity=mic_temp.stock, products.product_price=mic_temp.price;
Specify all columns of mic_temp which you want to insert or update in products table under
set statement.
Related
I'm trying to update an empty table notes, with values that come from a column in another table deals:
UPDATE notes
SET notes.content = (
SELECT deals.memo
FROM deals
WHERE deals.id = notes.deal_id
);
this runs with no error but no notes get updated although there are loads of values in memo.
There's no values at all in notes. Can this be the problem?
Perhaps you want to insert rows into notes:
INSERT INTO notes (deal_id, content)
SELECT d.id, d.ememo
FROM deals;
This will add rows into notes with values from the rows in deals.
I have a controller to save a record
My Table contains BELOW Fields
This is Must (It has to repeat in LOOP).
I want to insert a record single time in a table for a Employee Id, and it should not repeat again. But Same Employee can have multiple Batch Ids and Multiple Course ID's.
If I take Unique as a Employee Id that is not working again to insert the another record to the same employee.
This process should repeat inside the loop and I need to get the Last Inserted ID from the Table and have to assign the number of students in the another table. This everything is working fine if I create a Procedure in Mysql and If I call Procedure. But my Linux server is not executing and throwing MySQL error.
Here is my query and
<code>
$insert_staff_assign = "insert into staff_assign
(`main_course_id`, `main_batch_id`, `section`, `semester_course_id`, `emp_mas_staff_id`, `emp_category`)
VALUES
(:main_course_id, :main_batch_id, :section_id, :semester_course_id, :emp_mas_staff_id, :emp_category)
ON DUPLICATE KEY UPDATE
main_course_id=:main_course_id, main_batch_id=:main_batch_id, section=:section_id, semester_course_id=:semester_course_id, emp_mas_staff_id=:emp_mas_staff_id, emp_category=:emp_category ";
insert into staff_assign
(`main_course_id`, `main_batch_id`, `section`, `semester_course_id`, `emp_mas_staff_id`, `emp_category`)
VALUES
(:main_course_id, :main_batch_id, :section_id, :semester_course_id, :emp_mas_staff_id, :emp_category)
ON DUPLICATE KEY UPDATE
main_course_id=:main_course_id, main_batch_id=:main_batch_id, section=:section_id, semester_course_id=:semester_course_id, emp_mas_staff_id=:emp_mas_staff_id, emp_category=:emp_category
insert into staff_assign
(`main_course_id`, `main_batch_id`, `section`, `semester_course_id`, `emp_mas_staff_id`, `emp_category`)
SELECT * FROM (
SELECT
:main_course_id, :main_batch_id, :section_id, :semester_course_id, :emp_mas_staff_id, :emp_category
) AS tmp WHERE NOT IN (
SELECT emp_mas_staff_id FROM staff_assign WHERE emp_mas_staff_id = $save_emp_mas_staff_id
) LIMIT 1
</code>
Please send me the query to get rid of this problem.
The above are my queries.
I found the answer for the above question.
mysql.proc problem.
In my mysql my mysql.proc was corrupted. That is the reason it doesn't execute the above query.
To fix the above issue you need to update mysql in linux.
I have two table with column like this:
Table aggthndet (Reference table)
SELECT `aggthndet`.`idaggdet`,
`aggthndet`.`idagg`,
`aggthndet`.`noakun`,
`aggthndet`.`ketdet`,
`aggthndet`.`pagu`,
`aggthndet`.`prosesagg`,
`aggthndet`.`realisasi`,
`aggthndet`.`iu_id_usr`,
`aggthndet`.`iu_wkt`,
`aggthndet`.`iu_stat`FROM `aggthndet`;
Table aggakundet
SELECT `aggakundet`.`id`,
`aggakundet`.`idaggdet`,
`aggakundet`.`ketdetakun`,
`aggakundet`.`volume`,
`aggakundet`.`hrg_satuan`,
`aggakundet`.`iu_id_usr`,
`aggakundet`.`iu_wkt`,
`aggakundet`.`iu_stat`
FROM `aggakundet`;
The tables are mutually related to each other (relationship one-to-many)
i want insert data into table aggakundet, and update column pagu on table aggthndet, pagu column is the sum of the overall jml_total (alias column) of columns that have the same idaggdet.
sample data
table aggthndet
table aggakundet
In your script, you are having the value of idaggdet in $idaggdet.
Once insertion is done, proceed with UPDATE using the values in $idaggdet
You can take the below query as reference,
UPDATE `aggthndet`
SET `pagu` = `pagu`+1
WHERE `idaggdet` = '$idaggdet';
You can append this update statement in $sql itself.
I am trying to insert records into MySQL database from a MS SQL Server using the "OPENQUERY" but what I am trying to do is ignore the duplicate keys messages. so when the query run into a duplicate then ignore it and keep going.
What ideas can I do to ignore the duplicates?
Here is what I am doing:
pulling records from MySQL using "OpenQuery" to define MySQL "A.record_id"
Joining those records to records in MS SQL Server "with a specific criteria and not direct id" from here I find a new related "B.new_id" record identifier in SQL Server.
I want to insert the found results into a new table in MySQL like so A.record_id, B.new_id Here in the new table I have A.record_id set as a primary key for that table.
The problem is that when joining table A to Table B some times I find 2+ records into table B matching the criteria that I am looking for which causes the value A.record_id to 2+ times in my data set before inserting that into table A which causes the problem. Note I can use aggregate function to eliminate the records.
I don't think there is a specific option. But it is easy enough to do:
insert into oldtable(. . .)
select . . .
from newtable
where not exists (select 1 from oldtable where oldtable.id = newtable.id)
If there is more than one set of unique keys, you can add additional not exists statements.
EDIT:
For the revised problem:
insert into oldtable(. . .)
select . . .
from (select nt.*, row_number() over (partition by id order by (select null)) as seqnum
from newtable nt
) nt
where seqnum = 1 and
not exists (select 1 from oldtable where oldtable.id = nt.id);
The row_number() function assigns a sequential number to each row within a group of rows. The group is defined by the partition by statement. The numbers start at 1 and increment from there. The order by clause says that you don't care about the order. Exactly one row with each id will have a value of 1. Duplicate rows will have a value larger than one. The seqnum = 1 chooses exactly one row per id.
If you are on SQL Server 2008+, you can use MERGE to do an INSERT if row does not exist, or an UPDATE.
Example:
MERGE
INTO dataValue dv
USING tmp_holding_DataValue t
ON t.dateStamp = dv.dateStamp
AND t.itemId = dv.itemId
WHEN NOT MATCHED THEN
INSERT (dateStamp, itemId, value)
VALUES (dateStamp, itemId, value)
I need two id columns in the same table that create unique values upon insert. Since MySQL allows only one column to auto-increment, I need to do something like max(id)+1 for the other column.
Here's the SQL I expected to work:
INSERT INTO invoices (invoiceid)
VALUES ((SELECT MAX(invoiceid)+1 FROM invoices))
The select statement works independently, but within my INSERT, it's not allowed. I get the error : You can't specify target table 'invoices' for update in FROM clause
You want to use INSERT INTO .... SELECT FROM instead of INSERT INTO...VALUES():
INSERT INTO invoices (invoiceid)
SELECT MAX(invoiceid)+1
FROM invoices
My question for you would be why are you not use an AUTO INCREMENT field to generate the invoiceid value? That is what it is for, then you will not have to create this when inserting data.