Welcome to WebmasterWorld Guest from 54.145.173.36

Forum Moderators: open

Message Too Old, No Replies

Options for inserting and updating mysql

Options for inserting and updating mysql

   
6:34 pm on Jun 25, 2011 (gmt 0)

5+ Year Member



Is it possible to do something like this?

$sql = "UPDATE `test` SET (`name`,`age`) VALUES ('Sam','34') WHERE `id` = 10";

It would be nice because the insert command is similar

$sql = "INSERT INTO `test` (`name`,`age`) VALUES ('Sam','34')";
6:37 pm on Jun 25, 2011 (gmt 0)

5+ Year Member



Or vise versa, is there a way to

$sql = "INSERT INTO `test` (`name`='sam',`age`='34')";
8:10 pm on Jul 5, 2011 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Have you checked the optional syntax?
[dev.mysql.com...]
8:51 pm on Jul 5, 2011 (gmt 0)

5+ Year Member



not really seeing an answer there? Can you specify some code or be more specific?
9:06 pm on Jul 5, 2011 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Second syntax example is almost exactly what you typed in your second post ...

INSERT INTO test SET name = 'sam', age = 34;
9:35 pm on Jul 5, 2011 (gmt 0)

5+ Year Member



Ok, thank you, I wish they could write their examples like that. For some reason that mysql website is just so hard for me to read.

Somehow

INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name
SET col_name={expr | DEFAULT}, ...
[ ON DUPLICATE KEY UPDATE
col_name=expr
[, col_name=expr] ... ]

Just doesnt register in my brain.

thanks for you post and letting me know this is possible.
11:04 pm on Jul 5, 2011 (gmt 0)

5+ Year Member



This was exactly what I was looking for, this way I can re-use a portion of the query for inserting and updating.

Just out of curiosity at this point, is there a way to do something like this?

$sql = "UPDATE `test` SET
(
`name`,
`age`
)
VALUES
(
'Pete',
'34'
)
WHERE `id` = 10
";
12:32 pm on Jul 7, 2011 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



not according to the syntax ...
[dev.mysql.com...]

Regarding reading the manuals and understanding the syntax ...
[dev.mysql.com...]