Mysql 在插入时检查表中的最后一个条目 [英] Mysql check for last entry in table while inserting

查看:46
本文介绍了Mysql 在插入时检查表中的最后一个条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,它有 2 列 Action_key(存储 loggedinoptin 值)和 Value(存储 0 或 1)

I have a table which has 2 columns Action_key (which stores loggedin and optin values) and Value (which stores 0 or 1)

我的表有 Id 列自动递增.

My table has Id column auto increment.

更新

下面是我如何通过检查条件插入.

Below is how im inserting by checking the condition.

现在我需要条件来检查新插入的行是否与最后一条记录的 UidAction_keyValue 具有相同的值,否则它应该插入.

Now i need the condition to check if the newly inserting row has same value as the last record's Uid, Action_key and Value, else it should insert.

 $uniquedataoptininArray = array(   //except CreatedDate
        'Uid'=> $uid,
        'Action_key'=> $actionkey,
        'Value'=>$login_optin_value
        );
$userexists=  $this->getUserOptInLogTable()->getOptinLogindetails($uniquedataoptininArray);

if($userexists){

         echo 'record exist';

     }else{
          echo 'To be inserted';
          $result =  $this->getUserOptInLogTable()->insertdetails($dataArray);

下面的查询给了我最后插入的行 - 这很好.但需要知道如何使用下面的查询来检查条件

The below query gives me the last inserted row - Which is fine. but need to know how i can check the condition using rhe below query

$qry ="SELECT * FROM UserOptInLog WHERE Uid = ". $uid ." ORDER BY Id DESC LIMIT 1;";

当前问题.

应该插入的条件

1) 如果我的下表中填充了如图所示的值,现在我尝试插入带有 Uid 1608ActionKey 登录Value 1<的行/code>,但它没有插入.

1) If my below table is filled with values like shown, now im trying to insert a row with Uid 1608, ActionKey loggedin and Value 1, but it is not inserting.

2) 或者,如果我使用值 Uid 1608Action_key optinvalue 1 创建一个条目 - 它没有插入(应该插入理想)

2) Or if i make an entry with values Uid 1608, Action_key optin and value 1 - it is not inserting (which should insert ideally)

不应该插入的条件

1) 使用现有数据表 - 如果我插入 Uid 1608ActionKey 登录value 0 - 与最后一行基本相同行 - 它不应该插入

1) with the tables existing data- if i insert Uid 1608, ActionKey loggedin and value 0 - basically the same row as last row - it should not insert

推荐答案

如果您已经拥有 $this->tableGateway->getLastInsertValue() 中的 last idAction_key &"logged_in" &0 分别,那么就不用关心你的查询了...

If you already have last id from $this->tableGateway->getLastInsertValue() and Action_key & value as "logged_in" & 0 respectively, then there is no need to be concerned about your query...

  1. 只需检查与这 3 个项目(列)匹配的记录:

  1. Just check for a record where these 3 items (columns) match:

SELECT * FROM `table` 
WHERE `id` = <last_id>    # one you get from $this->tableGateway->getLastInsertValue()
AND `Action_key` = 'loged_in'
AND `value` = 0

  • 检查上面查询返回的记录数:

  • Check for the number of records returned from above query:

    • 如果返回 null,INSERT 你的新记录
    • 否则,请忽略

    <小时>

    更新

    1. 只需检查这 3 个项目(列)匹配的Uid:

    SELECT Uid FROM `table`   # get just Uid column
    WHERE `id` = <last_id>    # one you get from $this->tableGateway->getLastInsertValue()
    AND `Action_key` = 'loged_in'
    AND `value` = 0
    

  • 匹配上述查询返回的 Uid:

  • Match the Uid returned from above query:

    • 如果它匹配新的 Uid(将要插入的记录),INSERT 你的新记录
    • 否则,请忽略
    • if it doesn't match the new Uid (the record that is going to be inserted), INSERT your new record
    • else, Just Ignore

    <小时>

    还有

    您可以检查 Uid 的最后一个值:

    You can check for the last value for Uid:

    SELECT Action_key, value FROM table
    WHERE Uid = uid 
    ORDER BY id DESC LIMIT 1;
    

    这将返回目标 Uid 的最后一项.然后你可以匹配 Action_keyvalue 列来匹配记录...

    this will return the last item for the target Uid. Then you can match the Action_key and value columns for matching records...

    这篇关于Mysql 在插入时检查表中的最后一个条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆