php / MySQL条件IF ON DUPLICATE KEY UPDATE行中的多个单元

时间:2016-09-03 10:20:06

标签: php mysql

尝试使用下面的代码更新一行中的3个单元格。 on duplicate的第一部分工作正常,检查用户是否存在,只有得分更高才更新,但是,无论得分是否更高,后两个语句都会更新,它们似乎不包括在内有条件的IF statememnt。

// This query checks if a user exists and either enters or updates based on high score 
    $date_entered  = date('Y-m-d h:i:s');
    $query = mysql_query("INSERT INTO `hkhwa_entries` (player_id, name, week_no, score, date_entered, game_time) 
        VALUES ('$player_id', '$name', '$week_no', '$score', '$date_entered', '$gametime')
            ON DUPLICATE KEY UPDATE score = if(VALUES(score) > score, VALUES(score), score), date_entered = '$date_entered', game_time = '$gametime'");

我尝试了以下代码,但它只是完全打破了查询。

非常感谢任何帮助。

$query = mysql_query("INSERT INTO `entries` (player_id, name, week_no, score, date_entered, game_time) 
    VALUES ('$player_id', '$name', '$week_no', '$score', '$date_entered', '$game_time')
        ON DUPLICATE KEY UPDATE score = if(VALUES(score) > score, VALUES(score), score), 
        date_entered = if(VALUES(score) > score, VALUES(score), score, '$date_entered'), 
        game_time = if(VALUES(score) > score, '$game_time'");        

2 个答案:

答案 0 :(得分:0)

您还必须为其他两个作业指定IF:

INSERT INTO hkhwa_entries
     (player_id, name, week_no, score, date_entered, game_time)

VALUES
     ('$player_id', '$name', '$week_no', '$score', '$date_entered', '$gametime')

ON DUPLICATE KEY UPDATE
     score = IF(VALUES(score) > score, VALUES(score), score),
     date_entered = IF(VALUES(score) > score, VALUES(date_entered), date_entered),
     game_time = IF(VALUES(score) > score, VALUES(game_time), game_time)

您提供的第二个版本有语法错误

答案 1 :(得分:0)

@ user1903887试试这个

    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="60dp"
                app:layout_scrollFlags="scroll|enterAlways"
                android:text="View which must show up when scrolling starts"/>
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:background="#aa0a2f"
                app:layout_scrollFlags="scroll" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:text="pinned view">

            </TextView></android.support.design.widget.AppBarLayout>
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </android.support.design.widget.CoordinatorLayout>

date_entered = if(VALUES(得分)&gt;得分,VALUES(得分),得分,&#39; $ date_entered&#39;)

您在$query = mysql_query("INSERT INTO `hkhwa_entries` (player_id, name, week_no, score, date_entered, game_time) VALUES ('$player_id', '$name', '$week_no', '$score', '$date_entered', '$gametime') ON DUPLICATE KEY UPDATE score = if(VALUES(score) > '$score', VALUES(score),'$score'), date_entered = if(VALUES(score) > '$score', VALUES(date_entered),'$date_entered'), game_time = IF(VALUES(score) > '$score', VALUES(game_time), '$gametime'); 中使用了额外的score,