问题
I have a table which has 2 columns Action_key
(which stores loggedin
and optin
values) and Value
(which stores 0 or 1)
My table has Id
column auto increment.
Update
Below is how im inserting by checking the condition.
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;";
Current problem.
Conditions when it should insert
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) Or if i make an entry with values Uid 1608
, Action_key optin
and value 1
- it is not inserting (which should insert ideally)
Conditions when it should not insert
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
回答1:
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...
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:
- if it returns null,
INSERT
your new record - else, Just Ignore
- if it returns null,
UPDATE
Just check for the Uid where these 3 items (columns) match:
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
Match the Uid returned from above query:
- if it doesn't match the new Uid (the record that is going to be inserted),
INSERT
your new record - else, Just Ignore
- if it doesn't match the new Uid (the record that is going to be inserted),
ALSO
You can check for the last value for Uid:
SELECT Action_key, value FROM table
WHERE Uid = uid
ORDER BY id DESC LIMIT 1;
this will return the last item for the target Uid. Then you can match the Action_key
and value
columns for matching records...
回答2:
You may use such a statement with subquery
insert into tab
select q.*
from
(
select *
from tab
where date_ = ( select max(date_) from tab where Action_key = 'loggedin' )
and Value = 0
) q
where date_ is not null;
来源:https://stackoverflow.com/questions/53313411/mysql-check-for-last-entry-in-table-while-inserting