I have a stupid little SQL query which isn't working and i can't figure out why
SELECT * FROM `tablename` WHERE `id` = '294' INSERT INTO 'auth' VALUES 'false'
I want to select the row which pertains to the id (294) and insert the value false into the 'auth' column, but i keep getting a #1064 error.
Can someone correct the above?
embarrassed,
Dan
You just want to update the '294' record and set its 'auth' value to 'false', correct? If so:
update tablename set auth = 'false' where id = 294;
you want to do an update not an insert.
Update 'tablename' set 'auth' = 'false' where 'id' = '294'
Related
I need to log the updated columns into an existing empty column in which it should appear "Updated" or "Not Updated". I am running this query in MS Access.
Below find an example for my update query (which works) and code for the trigger (not sure i need one, or if i'm using it correctly)
update my_table
set col_i_need_to_set = 'value'
WHERE another_col like 'some_text'
and another_col2 LIKE 'some_other_text'
and another_col3 LIKE 'text'
CREATE OR REPLACE TRIGGER historyKeeper
AFTER UPDATE my_table
FOR EACH ROW
BEGIN
IF( UPDATING( 'col_i_need_to_set' ) )
THEN
INSERT INTO my_table( column_in_which_i_want_to_insert, column_value )
VALUES( 'Updated');
else
INSERT INTO my_table( column_in_which_i_want_to_insert, column_value )
VALUES( 'Not updated');
END IF;
END;
Thank you
actually, found the solution. after set col_i_need_to_set = 'value' I just need to add , column_in_which_i_want_to_insert = 'UPDATED' Maybe this will help someone
I am trying to insert some rows form a select statement. The select statement returns multiple rows but with each row insert I want to update a few other columns.
This is what I have so far
INSERT INTO {$this->db->dbprefix('term_response')}
SET `Crs Code` = (
SELECT `Crs Code`
FROM {$this->db->dbprefix('EnrolmentsList')}
WHERE `Person ID` = ?
),`term_id` = ?, `st_id` = ?
I'm getting 'Subquery returns more than 1 row'. Can I not do what I'm trying?
Thanks
If you want to insert static values for other fields you could do something like this:
INSERT INTO {$this->db->dbprefix('term_response')}
SELECT `Crs Code`, 'my term_id value', 'my st_id value'
FROM {$this->db->dbprefix('EnrolmentsList')}
WHERE `Person ID` = 12345;
I'm trying to do something like this inside of a stored procedure:
REPLACE INTO mytable
SET myid = `IDvalue`, mytitle = `sMyTitle`, myoptionalvalue = IFNULL(`sMyOptValue`, myoptionalvalue);
But not seems to work, any idea how to do this?
Thanks!
The REPLACE INTO syntax works exactly like INSERT INTO except that any old rows with the same primary or unique key is automaticly deleted before the new row is inserted.
This means that instead of a WHERE clause, you should add the primary key to the values beeing replaced to limit your update.
REPLACE INTO myTable (
myPrimaryKey,
myColumn1,
myColumn2
) VALUES (
100,
'value1',
'value2'
);
...will provide the same result as...
UPDATE myTable
SET myColumn1 = 'value1', myColumn2 = 'value2'
WHERE myPrimaryKey = 100;
The thing that i want to do is to check if there is a same row and if not to insert a new row to the table.
In that case, how can i use if not exists ?
INSERT INTO `facebook` (`ID`,`fb_id`,`label_id`,`page_ids`,`token`) VALUES (NULL,'". $session['id'] ."','$id','','". $access_token ."')
For example, in that query, i want to check if there is a row with the same label_id record and if not, execute that query.
any help ?
thanks
You can use the INSERT IGNORE INTO ... syntax. When using this, duplicate key errors are treated as warnings and the statement will return without having inserted the affected row.
INSERT INTO `facebook` (`ID`,`fb_id`,`label_id`,`page_ids`,`token`)
SELECT NULL, '". $session['id'] ."', '$id', '', '". $access_token ."'
FROM DUAL
WHERE NOT EXISTS (SELECT 'x' FROM facebook WHERE label_id = '$id')
I think FROM DUAL is optional, but I'm an Oracle guy. :)
User Insert Into to ignore duplicates:
INSERT IGNORE INTO `facebook`
SET `ID` = null,
`fb_id` = '". $session['id'] ."',
`label_id` = '$id',
`page_ids`='',
`token`='". $access_token ."';
From my previous question this was the solution I am accepting:
INSERT IGNORE INTO table_name
SET name = 'phill',
value = 'person',
id = 12345;
But I also wanted to know if I could be multiple values like this
INSERT IGNORE INTO table_name
SET (name = 'phill', value = 'person', id = 12345),
(name = 'bob', value = 'person', id = 12346);
INSERT IGNORE INTO table_name(name,value,id) VALUES
('phill','person',12345),('bob','person',12346)