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.
Related
i had query like this
CREATE TRIGGER `tambah_riwayatobat` AFTER INSERT ON `obat`
FOR EACH ROW insert into riwayat_obat(nama, keterangan, distributor,tanggal)
(select new.nama, 'Masuk', d.nama ,now()
From distributor d
join obat ON new.id_distributor = d.id_distributor)
i try to insert data with trigger and one of part data i fetch with constraint, but why the data be duplicate entry ?
Output :
example, if i try to insert data obat 1st time, data on tambah_riwayatobat insert 1 too
if i try to insert data obat 2nd time, data on tambah_riwayatobat insert 2 times with same data
if i try to insert data obat 3rd time, data on tambah_riwayatobat insert 3 times with same data
I'm not certain exactly what's happening, but it's a result of the join in your trigger code. You’re joining obat to distributor, but your join condition makes no mention of obat so you're getting some sort of cross-product where on the second and subsequent INSERT your SELECT subquery is selecting more than one row.
You shouldn't (and don't need to) use the join, since all the data you need from obat is already in the pseudorecord NEW. The following code should work much better:
CREATE TRIGGER `tambah_riwayatobat`
AFTER INSERT ON `obat`
FOR EACH ROW
INSERT INTO riwayat_obat
(nama, keterangan, distributor, tanggal)
(SELECT NEW.nama, 'Masuk', d.nama, now()
FROM distributor d
WHERE new.id_distributor = d.id_distributor
LIMIT 1);
The LIMIT clause will ensure that the SELECT selects only one row, so the INSERT inserts only one row; if distributor.id_distributor is a primary key the LIMIT clause is unnecessary.
How can i copy table and insert new value at the same time
I want to copy a table with multiple rows and insert a same value "Borrowed" for one column Transaction. I already know how to copy but i dont know how to copy and insert new/another value at the same time.
here is what i got:
INSERT INTO TRANSACTION(UserID,TRANSACTION,First_Name,Last_Name,ISBN,Title,DATE)
VALUES (1,"Borrowed",(SELECT First_Name,Last_Name,ISBN,Title),NOW());
Add your fixed values into the SELECT list:
INSERT INTO TRANSACTION(UserID,TRANSACTION,First_Name,Last_Name,ISBN,Title,DATE)
SELECT 1, "Borrowed", First_Name,Last_Name,ISBN,Title,NOW()
FROM <your table goes here>
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.
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.
I had posted other questions relating to this problem, but haven't had any responses to directly address the issue of multiple row data imports from XLS. I'm an infrequent user of SQL or DBs in general, so my background/experience is limited in regard to writing these queries. If there is an easier or more direct approach to reach my goal, I'm certainly open to them. I don't mean to overpost or anything, but this site seems to be the most helpful (thank you everyone who has replied to my other post).
From some posts I have looked at, I understand that I have a working set-based query/trigger (since multiple rows do get imported). Ultimately I only need to import data into the parent table, and the child table can be populated with static values and or values from the parent table, but the PK/FK relationship needs to be maintained. And this is what I seem to have the most trouble with when more than 1 row of data is imported from XLS.
I have set up a trigger to insert values into a child table when a insert is executed on the parent table. The query executes correctly however I am unable to have the FK match the PK when multiple rows of data are inserted. The FK always has the ID of the last row inserted in the parent table. I have tried several approaches from other forum posts (here and on other sites) but always get errors.
Here is my updatePgVer Trigger code:
ALTER TRIGGER [updatePgVer]
ON [prototype].[dbo].[PageVersion]
FOR INSERT AS
BEGIN
SET NOCOUNT ON;
-- Insert into PageHistory
INSERT
INTO [prototype].[dbo].[PageHistory] ([VersionID], [Date], [Action], [Who], [StateId], [Owner])
SELECT
##IDENTITY
, GETDATE()
, 'created'
, 'xls_user'
, [StateID]
, 'xls_user'
FROM inserted
END
And the query used to insert into the parent table:
INSERT INTO [prototype].[dbo].[PageVersion] ([Number], [PageId], [Properties], [StateId], [Language], [SearchText], [PageMetaDescription], [PageMetaKeyWords], [PageTypeId], [Name], [Title], [Owner], [Admin], [ShowInMenu])
SELECT [Number], [PageId], [Properties], [StateId], [Language], [SearchText], [PageMetaDescription], [PageMetaKeyWords], [PageTypeId], [Name], [Title], [Owner], [Admin], [ShowInMenu]
FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=C:\test_import.xls', 'SELECT * FROM [Query$]');
The only other idea I have would to be to create some sort of loop that goes through each row and imports 1 at a time, so that the ##IDENTITY will always match. However, examples I have looked at seem hard to apply to my import.
The value for column VersionID, which appears to be the column with IDENTITY, is in the inserted table. You can reference it in your trigger like this
INSERT
INTO [prototype].[dbo].[PageHistory] ([VersionID], [Date], [Action], [Who], [StateId], [Owner])
SELECT
[VersionID],
, GETDATE()
, 'created'
, 'xls_user'
, [StateID]
, 'xls_user'
FROM inserted
If you want to see what data is available from inserted during the INSERT, temporarily put this in your trigger:
SELECT *
FROM inserted