I'm running this query in 2 mysql_query calls. I would like to do the most performant thing and run it in one query. Can anyone optimize this for me:
if(mysql_num_rows(eq("select *
from player_data
where uid = $uid")) == 0 )
eq("update开发者_如何学Go player_data
set uid = $uid,
`stat`= REPLACE(`stat`, '$temp_uid', '$uid')
where uid = $temp_uid");
Note: eq is just a wrapper function for mysql_query
You could try:
eq("update player_data
set uid = $uid,
`stat`=REPLACE(`stat`, '$temp_uid', '$uid')
where uid=$temp_uid
and not exists (select *
from player_data
where uid = $uid)");
if(mysql_num_rows(eq("select 1 from player_data where uid=$uid")) == 0 )
eq("update player_data set uid=$uid, `stat`=REPLACE(`stat`, '$temp_uid', '$uid') where uid=$temp_uid");
Avoid select *
if you just want to get the count of rows in the table.
Will A's answer is very good, and if you do need to select a field specify it. Selecting everything is terrible for performance.
精彩评论