开发者

Trying to optimize this MySQL query (select, insert and update in the same query)

i have this query from php:

mysql_query("INSERT INTO ".table_hack." (id,ip,count,lasttime) 
             VALUES (NULL,'".$ip."',1,NOW()) 
             ON DUPLICATE KEY U开发者_运维技巧PDATE count=count+1, lasttime=NOW();");

As you can see im inserting a new record on the table_hack only if the Unique element is not present that in my case is ip. If the element is present the query updates de value of count in 1 and lasttime with the NOW() parameter.

I want also to know the value of count in the same query, it is possible?. I know that i could make another query but i will like to make it just in one (if is possible). Thanks for any help!


Nope, you can't.
Also, I see no reason for such a desire.

To me, it's interesting phenomenon. Such questions are quite often on Stackoverflow and I am curious why people so eager to combine queries into one call. And even more surprising fact that everyone are limiting themselves with just 2 queries, and nobody asks how to combine ALL the queries from the script into one mysql call.


You can't combine an INSERT and SELECT query so you'll have to run two queries. Why would it be a problem to use two queries?


You can not do it with standard php mysql functions. You can do it with Mysqli driver and method multi_query(), but it will be a 2 queries whatever.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜