INSERT INTO fields (id_region, id_fields_info, subsidy_dka, id_rents_dka, type_uses, id_rented_from, id_categories, id_farmer, id_season)
SELECT
regions.id,
fields_info.id ,
120,
rents_dka.rent_dka,
"собствена",
rented_froms.id,
categories.id_category,
farmers.id,
seasons.id
FROM regions, fields_info, rents_dka, rented_froms, categories, farmers,
seasons
WHERE
region = "Азмък" AND
field_num = 2222 AND
rent_dka = 60 AND
name = "Десислав" AND
id_category = 3 AND
name = "Десислав" AND
season = "2012-2013"
So I have these tables:
regions,
fields_info,
rents_dka,
rented_froms,
categories,
farmers,
seasons
and they are filled with some data.
I've made a form where the user fills the fields with data from these tables, that I've mentioned, and when the submit button is clicked I want to fill table FIELDS in MYSQL with ID's which I get from the data, the user had entered.
to spot the problem, I'd proceed as follow:
execute an insert without cyrillic
remove all the and
make a default insert with default values
if you get "0 rows inserted" it means that the syntax is correct, but the where clause fails to find any matching entry. I suspect the problem is the AND with the cyrillic. Remove the ANDs until the query finds some entries
I got this case when i tried to insert into a table having a column unique and my SQL had the keyword IGNORE in it :
The query:
INSERT IGNORE INTO users SET `user_id` = 7321, `name`= 'test_name', `phone` = '+188888888';
If the query didn't have IGNORE, it would throw an error (because the column user_id was set unique and I already had a row with the same user_id) but due to IGNORE keyword it just ignores the query and hence results in 0 rows inserted.
Note: I know the question asked doesn't has IGNORE key in the query, but might help someone.
Related
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.
The Userdatabase has usernames with "?" ending on the name. For example username: "Alex?"
Instead of deleting it I'm trying to replace this "?" with a "2" to avoid duplicate entries. The Problem is, there a still duplicate entries even with 2 at the end. I need a query, which automatically changes the 2 to 3,4,5,6,7,8 or 9 until no duplicate entry exists anymore. I was doing this manually until now, but honestly I changed over 200 lines and I guess there are more than 1000.
Some Ideas?
The Query I use:
UPDATE `userdatabase`
SET `username` = replace(`username`, "?","2")
i am not familiar mysql update syntax in detail, but if table "userdatabase" have unique id column may be possible something like that
replace(username, "?", (select count(*) from userdatabase db where db.username = username and db.id < id))
I'm using a third party mysql table (ie I can't change any of its properties) and I have a row that has id (key), name and value.
I want to store unique cache keys into a row with the name cacheKeys.. and this is my sql statement
$query = "INSERT INTO ".$tableName." (name, value) VALUES ('CacheKeys', '".$key."') ON
DUPLICATE KEY UPDATE value = CONCAT_WS (',', $tableName.value, '$key')";
I've already implemented my caching algorithm, so that every time someone adds a cache key, I check to see if it already exists (from the CacheKeys row above), if it does I fetch it from cache.. otherwise I store it.
Problem is it seems that the sql write operation takes time, and it often stores duplicate cacheKeys
ie: currencies,defaultCurrencyId,user19,currency1,currency1,currency1,currency1,currency1
So I need to check to see that I'm not adding a duplicate key into the cacheKeys field.. and I need to do that using SQL (using php, ie regex etc would just be waaaay to expensive).
Try this::
INSERT INTO tb (firstname, lastname) VALUES ('Jack', 'Doe') IF NOT
EXISTS ( SELECT * FROM tb WHERE firstname='Jack' AND lastname='Doe' );
Im trying to insert 'testing' into my MeetingNotes column under two conditions but for the life of me I cannot get it to work. Is it possible to do this? I am a beginner with sql and mysql? Thanks in advance!
SELECT MeetingNotes
FROM Meeting
INSERT INTO MeetingNotes
VALUES ('testing')
WHERE MeetingProcessId = '1001' AND MeetingId = '25'
You want to use an UPDATE query, which changes values in existing records. An INSERT query strictly adds new records.
UPDATE Meeting
SET MeetingNotes = 'testing'
WHERE MeetingProcessId = '1001' AND MeetingId = '25'
For future reference, I'm not sure why you have a SELECT statement in your example: it isn't needed to insert or update records. Inserting a new record into the Meeting table (given only the three columns shown) would look like this:
INSERT INTO Meeting (MeetingId, MeetingProcessId, MeetingNotes)
VALUES ('25', '1001', 'Notes about this very exciting meeting...')
A couple notes on this:
Since INSERT statements add an entirely new record to the table, columnwise constraints can't be applied, so they don't support a WHERE clause
If MeetingId is an auto-incrementing record ID generated by the database, it should be / must be left out of INSERT statements
Only string (CHAR/VARCHAR) values should be quoted when they appear in queries, numeric values should not. So if, for example, MeetingId and MeetingProcessId are integer instead of string columns, the quote-marks around 25 and 1001 in the queries above should be removed
What you want is probably:
UPDATE Meeting SET MeetingNotes='testing' WHERE MeetingProcessID = '1001' AND MeetingId = '25';
I have the following SQL statement which was working perfectly until I moved it to another server. The middle query (encapsulated in ** ) does not seem to work. I am getting an error saying that 'AUTO' is an incorrect integer type. If I remove it altogether, it says that I have an incorrect number of fields. I am trying to copy data from one table to another and allow the destination table to auto increment its ID number.
SET sql_safe_updates=0;
START TRANSACTION;
DELETE FROM shares
WHERE asset_id = '$asset_ID';
/*************************************************************/
INSERT INTO shares
SELECT 'AUTO', asset_ID, member_ID, percent_owner, is_approved
FROM pending_share_changes
WHERE asset_ID = '$asset_ID';
/*************************************************************/
DELETE FROM pending_share_changes
WHERE asset_ID = '$asset_ID';
DELETE FROM shares
WHERE asset_ID = '$asset_ID' AND percent_owner = '0';
COMMIT;";
Based on this page of the mysql docs, you have to do:
INSERT INTO shares
(column_name1, column_name2, column_name3, column_name4) -- changed!
SELECT asset_ID, member_ID, percent_owner, is_approved
FROM pending_share_changes
WHERE asset_ID = '$asset_ID';
The difference is that the column names of the "receiving" table are explicitly listed after the name of the receiving table.
The docs say
AUTO_INCREMENT columns work as usual.