Inserting a single value into a column with a where clause - mysql

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';

Related

SQL Error when trying to insert one value into one row for a column

I'm using MySQL 5.7 and for some reason my INSERT statement isn't working as before even though the syntax looks correct. It's error-ing out on the where statement...
SQL:
insert into users(age) values('16') where username='r';
If the row for username r already exists perhaps you are looking to update the age value instead?
Use: update users set age = 16 where username = 'r' instead.
Also, I'm just guessing here, but maybe age holds a numeric value, and if so you can remove the quotes around 16.
That syntax isn't correct. You can't use where like that. Perhaps you want something like:
insert into users (username, age)
values ('r', '16')
http://dev.mysql.com/doc/refman/5.7/en/insert.html
Alternatively if that user already exists, you might be looking for an update statement instead:
update users set age = '16' where username = 'r'
INSERT statements must not contain a WHERE clause. Remove it.
If, in fact what you are trying to do is update an existing row, use an UPDATE statement, not an INSERT statement.
update users set age = '16' where username='r';
If you want to insert new records in your table, you have to write query for inserting data.
SQL INSERT INTO Statement syntax is this:
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
In your case, if you don't have the record in your database, your query will look like this:
INSERT INTO users (username, age)
VALUES ('r', '16')
But if you want to update existing records in your table, you have to write query for updating data using the SQL UPDATE Statement.
The syntax for this is:
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
To update the record/s, you have to specify clause in WHERE which record should be modified.
To modify age of user/s with username that is equals 'r', this is the query:
UPDATE users SET age = 16 WHERE username = 'r'
but if you want to modify for all users which usernames starts with 'r':
UPDATE users SET age = 16 WHERE username = 'r%'
I hope this explanation will help you to understand better SQL statements for INSERT new and UPDATE existing records.

mysql error in WHERE clause

I'm sorry to ask such a basic question, but I cant for the life of me spot the error here as far as I can see everything is correct. Yet I get the error, perhaps I need a pair of fresh eyes to have a look
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 'WHERE event_id = '1243'' at line 1 in INSERT INTO expiredEvents
(event_id,sport_type,tournament,round,team1,team2,venue,event_date)
values ('1243','Rugby','Super15','3','Waratahs','Sharks','Allianz
Stadium','') WHERE event_id = '1243'
$sql="INSERT INTO expiredEvents
(event_id,sport_type,tournament,round,team1,team2,venue,event_date)
values ('$id','$sport','$trnmnt','$rnd','$t1','$t2','$ven','$eDate')
WHERE event_id = '$id'"
There is no WHERE clause in the correct syntax of the INSERT statement.
Depending on what you want to achieve, choose one of the following.
Insert a new row, don't bother if another one having the same event_id already exists
INSERT INTO expiredEvents
(event_id, sport_type, tournament, round, team1, team2, venue, event_date)
VALUES
('$id', '$sport', '$trnmnt', '$rnd', '$t1', '$t2', '$ven', '$eDate')
If event_id is an UNIQUE INDEX of table expiredEvents, this query fails if another record having event_id = '$id' already exists.
Assuming event_id is the PK of the table, keep reading.
Insert a new row but only if it does not already exists
INSERT IGNORE INTO expiredEvents
(event_id, sport_type, tournament, round, team1, team2, venue, event_date)
VALUES
('$id', '$sport', '$trnmnt', '$rnd', '$t1', '$t2', '$ven', '$eDate')
The IGNORE keyword turns the errors into warnings and the query completes successfully but it does not insert the row if another one having event_id = '$id' already exists.
Inserts a row if it does not exist or update the existing one, if it exists
INSERT INTO expiredEvents
(event_id, sport_type, tournament, round, team1, team2, venue, event_date)
VALUES
('$id', '$sport', '$trnmnt', '$rnd', '$t1', '$t2', '$ven', '$eDate')
ON DUPLICATE KEY UPDATE
sport_type=VALUES(sport_type), round=round+1, event_date=NOW()
If the row does not exist, this query insert it using the values from the VALUES clause. If the row already exists then it uses the ON DUPLICATE KEY UPDATE to know how to update it. There are three fields modified in this query:
sport_type=VALUES(sport_type) - the value of column sport_type is updated using the value provided in the query for column sport_type (VALUES(sport_type), which is '$sport');
round=VALUES(round)+1 - the value of column round is updated using its current value plus 1 (round+1); the value provided in the VALUES clause is not used;
event_date=NOW() - the value of column event_date is modified using the value returned by the function NOW(); both the old value and the one provided in the VALUES clause of the query are ignored.
This is just an example, you put there whatever expressions you need to update the existing row.
Completely replace the existing row with a new one
REPLACE INTO expiredEvents
(event_id, sport_type, tournament, round, team1, team2, venue, event_date)
VALUES
('$id', '$sport', '$trnmnt', '$rnd', '$t1', '$t2', '$ven', '$eDate')
The REPLACE statement is a MySQL extension to the SQL standard. It first DELETEs the row having event_id = '$id' (if any) then INSERTs a new row. It is functionally equivalent with DELETE FROM expiredEvents WHERE event_id = '$id' followed by the first query exposed above in this answer.
WHERE keyword is not allowed in INSERT INTO / VALUES
http://dev.mysql.com/doc/refman/5.6/en/insert.html
Elaborate what you would like to accomplish.
Don't use where in insert statment, instead of just get the data from the respective tables which contains the exact records and insert necessary rows alone by adding your conditions in the select statement of that query.
Here is the simple one to resolve your error,
$sql="INSERT INTO expiredEvents
(event_id,sport_type,tournament,round,team1,team2,venue,event_date)
SELECT event_id,sport_type,tournament,round,team1,team2,venue,event_date from events WHERE event_id = '$id'"

insert statement sql inserting values into a table in mysql with a where clause condition

I have rails application and its using mysql.
I have a piles table with two columns that I care for.
The columns are name_he_il and name_en_us
I don't have a problem doing these
select name_he_il from piles;
select name_en_us from piles;
I need to insert data into the name_he_il column into the piles table where name_en_us = "a specific value"
I tried something like this
insert into piles (name_he_il) values 'לא מאפיין כלל' where name_en_us = "Extremely Uncharacteristic";
I am getting syntax error.
I was googling and I figured the sql should be
insert into table (column 1) values (blah) where conditions;
but its not working.
Basically that hebrew text means extremely uncharacteristic.
You want to use UPDATE ... WHERE
INSERT is for creating new records only.
Do UPDATE and not INSERT:
UPDATE piles SET name_he_il = 'לא מאפיין כלל' WHERE name_en_us = "Extremely Uncharacteristic";
Insert query is to insert new rom into table. If you already have row with value of "name_he_il" column then you need to use update.
UPDATE piles SET name_he_il='new value' WHERE name_he_es = 'something'

MYSQL INSERT - "0 rows inserted". Any suggestions why is that?

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.

Is this a Efficient way to query relational tables on MySQL?

I'm dealing with a relational table and I've been wondering if there's a way to lower the number of queries I need to make when inserting data to the tables..
Here are the queries I currently use:
I insert the "main" values.
INSERT INTO products
(title, description, status, url)
VALUES
('some title', 'description of doom', 1, 'some-title');
We make it insert the value only if it doesn't exist already.
INSERT IGNORE INTO values
(value)
VALUES
('example value');
Since I'm not sure if the query was actually inserted, I get the id..
SELECT id
FROM
values
WHERE
value = 'example value';
Where "?" is the ID I got from the last query.
INSERT INTO link
( id_product, id_catalog, id_value )
VALUES
( 33, 1, ? );
This means that each extra value I need to add will cost 3 queries. So my question is: Is there a more efficient way to do this?
You can do this to at least drop one of the queries:
INSERT INTO link
( id_product, id_catalog, id_value )
VALUES
( 33, 1, (SELECT id
FROM values
WHERE value = 'example value') );
I basically am replacing the '?' with a sub select of the second query to get the id.
"Is there a more efficient way to do this?"
No. Not really. Creating three things takes three inserts.
You should be able to tell whether the insert succeeded with the ROW___COUNT() function from inside MySQL. If calling from another language (e.g. PHP), the mysql_query or equivalent function will return the row count.
You could use an INSERT INTO ... ON DUPLICATE KEY UPDATE statement.
This does, however, require that the primary key be one of the values for the insert, so it doesn't work on tables with an auto-increment.