I want to insert into a table where user id = to something
And change o to 1 which means user is online
Tried this
mysql_query("INSERT INTO `20s` VALUES('','','','','','','',1) WHERE `uid`='$user_id ");
But that doesn't get me anywhere. What's the right syntax?
Also what's the best way to keep a record of online friends in the database?
TIP:it's better to use update here
Correct syntax for insert query is:
INSERT INTO table_name
VALUES (value1, value2, value3,...)
but you need here is update query, so you may update already existing row:
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
You only need to change one column bit that show online so dont change other columns:
UPDATE `tableName` SET `columnForOnline`=1 WHERE user_id=online_user_id
There is no WHERE clause in INSERT statements (see MySQL documentation).
If you want to update a value in an existing row, use UPDATE:
UPDATE `20s` SET `online`=1 WHERE user_id=your_user_id
mysql_ functions are deprecated, use PDO or mysqli_ instead!
MySQL also supports REPLACE INTO, which follows the same syntax as INSERT. Be careful though, columns that you do not supply will be set to their defaults.
In your case:
REPLACE INTO `20s` VALUES('','','','','','','',1) WHERE `uid`= '$user_id'
(Plus, you are missing a closing quote ' at the end of your query)
Use update query instead of insert
UPDATE `tableName` SET `columnForOnline`=1 WHERE user_id=online_user_id
Related
I know that the UPDATE statement has the following format:
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
I was wondering whether there exists a format analogous to INSERT statement, something like this:
UPDATE table_name
SET (column1,column2,column3,...)
VALUES (value1,value2,value3,...)
WHERE some_column=some_value;
Is this a valid query?
I'm using MySQL.
The short answer - no.
All the variants of the update syntax have a set clause of the form colulmn1=..., column2=... etc. For a complete list of the supported syntax variants, you can check out the documentation.
I'm personally prefer to use the following syntax:
INSERT INTO tab_name
SET
col1 = value1,
col2 = value2
I prefer this syntax as it gives me the full control over the correspondence between column names and the assigning values. It is better visualized from my point of view.
However most of the examples and howto's in the Internet (and on the Stack Overflow as well) uses the INSERT VALUES syntax like the following:
INSERT INTO tab_name (col1,col2) VALUES (value1,value2)
I'm agree that it can be slightly shorter. But in spite of this I hardly imagine how this can be handy. Especially if the number of inserting (updating) columns is more than 3 or 4. It can easily lead to mistakes if I will need to add or exclude one or more columns to/from the query.
So the question is: is there any reason to use INSERT/UPDATE () VALUES () syntax over INSERT/UPDATE SET?
Because the SET
INSERT INTO tab_name
SET
col1 = value1,
col2 = value2
is not SQL standard.
If you have to use other DBMS you can not use this syntax.
The first is SQL standard, the second is MySQL's extension.
Feel free to check also:
MySQL INSERT INTO table VALUES.. vs INSERT INTO table SET
I am using MySQL database. I want to insert data into it. But one column data contains special characters. (backslash) I want to replace it with Double backslash and then execute Insert query.
Can we do it using Insert Query?
While looking for the answer I came across
UPDATE your_table
SET your_field = REPLACE(your_field, '/', '//')
WHERE your_field LIKE '%articles/updates/%'
So it is possible use Replace in Update query.
Can we do the same in insert query? Please Let me know if you can help me.
You can use the following:
REPLACE INTO table_name(column_name1,column_name2,…)
VALUES(value1,value2,…)
More info from:
http://www.mysqltutorial.org/mysql-replace.aspx
You can use a BEFORE INSERT TRIGGER on your table
CREATE TRIGGER trigger_name
BEFORE INSERT
ON my_table
FOR EACH ROW
SET NEW.col_name = IF(NEW.col_name = '/','//',NEW.col_name);
Currently I need to remove some rows in my DB (MySQL) and don't matter for me if this row doesn't exist in the database. In other words, I need delete rows ignoring the errors.
I know the following command:
INSERT IGNORE INTO MyTable (field1, field2) values (value1, value2);
That insert in my database the values, regardless if it already exist or not. I already go to MySQL Reference but I didn't find a good answer to this question. Also, I see some questions in StackOverflow, like this, that don't helped me.
So, exist in MySQL a command like below?
DELETE IGNORE from MyTable where myField = myValue;
Regular delete:
DELETE from MyTable where myField = myValue;
will delete all rows matching condition myField = myValue. Situation when there are no such rows is not an error and simply nothing will be deleted.
I need to write an SQL query for MySQL so that a row is updated if it exists, but inserted if it does not.
i.e.
If row exists...
UPDATE Table1 SET (...) WHERE Column1='SomeValue'
If it does not exist...
INSERT INTO Table1 VALUES (...)
Can this be done in one query?
i believe you need to reverse your logic in order for it to work:
insert into a table - if it exists (same key) then update it.
this can be achieved by the ON DUPLICATE statement like so:
INSERT INTO Table1 VALUES(...)
ON DUPLICATE KEY UPDATE column=column+1
check the manual here
Use the INSERT... ON DUPLICATE KEY UPDATE syntax.
See the manual
(For searching purposes, btw, this is usually referred to as an "upsert")