Jump to content

Deleting Row


searls03

Recommended Posts

I would like to run this query:

$sql123 = "delete from holiday_cart where cart_id='$cid' && code='$code'" 
 ;
$rs = mysql_query($sql123) or die ("Problem with the query: $sql<br>" . mysql_error());

but what I need to know how to do, is if this query matches multiple results, how can I make it so that it only deletes one of the results instead of all of them?

Link to comment
Share on other sites

So, you are saying that there are multiple records in that table with the same cart_id and code and you want to delete only one of those records?

 

Well, based upon your initial query it is as simple as putting a LIMIT on the query

DELETE FROM holiday_cart
WHERE cart_id='$cid' AND code='$code'
LIMIT 1

 

However, now you are saying you only want to delete the most recent matching record based upon a timestamp field. So, in addition to the ORDER BY you would also want to specify the ORDER

DELETE FROM holiday_cart
WHERE cart_id='$cid' AND code='$code'
ORDER BY date_field DESC
LIMIT 1

Link to comment
Share on other sites

if you insist on not having a UID field then you could try something along the lines of :

WHERE ... AND timestampFieldName = (SELECT MAX(timestampFieldName) from tableName)

where ... is your current where condition. No promises though.

 

That's not really a good solution as it is not precise. If the records with the same criteria were created at the same time it's possible that they would have the same timestamp and the above logic would delete multiple records. Besides, a sub-query isn't necessary.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.