0
  • 'process' is a method for prepared statement
  • 'QId' & 'UnqId' are the child foreign keys (just a index)

i want to make it one line statement (including 3 queries) of mysql using subquery or etc

$get = $call->process("SELECT UnqId FROM table1
                     WHERE QId = ? AND UnqId = ?", 
                     array($_SESSION['Q'], $_SESSION['U']));
if($get) //if table exists
{
$call->process("UPDATE table1 SET col3 = ?, col4 = UTC_TIMESTAMP() 
              WHERE QId = ? AND UnqId = ?", 
                  array('OK', $_SESSION['Q'], $_SESSION['U']));
}
else
{
$call->process("INSERT INTO table1 VALUES (?, ?, ?, UTC_TIMESTAMP(),
               NULL, NULL, NULL, NULL)", 
                  array($_SESSION['Q'], $_SESSION['U'], 'OK'));
}
5
  • If you're using mysql you could take a look at INSERT ON DUPLICATE KEY UPDATE syntax, it's a beautiful thing! But be aware that if you shift database that this is mysql only Commented Aug 1, 2013 at 8:30
  • @Dale but how can i make one line query of three queries? including SELECT statement.. Commented Aug 1, 2013 at 8:35
  • I'm not sure if you can, you would run your select first then create a one line insert / update based on the link documentation in my previous comment Commented Aug 1, 2013 at 8:36
  • @Dale there is 'EXISTS' thing in subquery of sql but i don't know how to use it.. Commented Aug 1, 2013 at 8:39
  • That's what google is for my friend, good luck Commented Aug 1, 2013 at 8:44

2 Answers 2

1

You can use a query that uses ON DUPLICATE KEY UPDATE

INSERT INTO table (a,b,c) VALUES (1,2,3)
  ON DUPLICATE KEY UPDATE c=c+1;
Sign up to request clarification or add additional context in comments.

7 Comments

but how to combine 'select' statement with other queries
Is your select only to check if the record is there right?
yes... should i use 'EXISTS' thing of subquery of sql
so you don't need to use the select in this case the ON DUPLICATE KEY UPDATE takes care of that for you
Of course you have to create unique index with this two fields (QId and UnqId).
|
1

Check out ON DUPLICATE KEY UPDATE syntax to do it in one query. And try to search next time, eg. "mysql update on duplicate".

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.