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
https://forums.phpfreaks.com/topic/270344-deleting-row/
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
https://forums.phpfreaks.com/topic/270344-deleting-row/#findComment-1390565
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
https://forums.phpfreaks.com/topic/270344-deleting-row/#findComment-1390569
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.