Jump to content

[SOLVED] mysql delete


phorcon3

Recommended Posts

mysql table:

 

id

name

check

reply

 

1

test1

1

1

 

2

test2

[/td]

[td]1

 

and now i wanna have a cronjob, and i wanna do the following

 

first of all i have to group reply, meaning they must have the same ID number

 

and then i wanna delete those, but ONLY then when ALL those rows, with the same reply ID number ALSO have a 1 in the check column

 

does that make sense?

 

i tried, the following

 

<?php
$a = mysql_query("SELECT `reply` FROM `table` WHERE `check` = '1' GROUP BY `reply`");
while($b = mysql_fetch_assoc($a))
{
mysql_query("DELETE FROM `table` WHERE `reply` = '$b[reply]'");
}
?>

 

but it would delete ALL rows with the same reply ID number, even tho i ONLY want to delete them when ALL of them have check = 1, but its ignoring it, and i suppose its because of GROUP BY?

 

i hope this makes sense lol

Link to comment
Share on other sites

lol ...i must really suck at explainin things

 

1st row:

ID: 1

Name: test1

Check: 1

Reply: 1

 

2nd row:

ID: 2

Name: test2

Check: 0

Reply: 1

 

3rd row:

ID: 3

Name: test3

Check: 0

Reply: 3

 

so, what i wanna do is, group all rows ..which have the same reply ID number, which in this case would be the [1st and 2nd row] and the [3rd row]

 

and then i wanna check, if both have a 1 in their check column ...and if BOTH and ONLY then have a 1 in there, i wanna delete them

 

if just ONE has a 1 in their check column (like in my example above) nothing should happen

 

does it make sense now? lol

Link to comment
Share on other sites

 

 

<?php

$a = mysql_query("

            select q1.reply

            from (

                    select reply, count(*) as data_cnt from sample where 1 group by reply

                  ) as q1

          left join (

                    select reply, count(*) as check_cnt from sample where `check`=1 group by reply

          ) as q2

                  on q1.reply=q2.reply

          where data_cnt = check_cnt

      ");

while($b = mysql_fetch_assoc($a))

{

mysql_query("DELETE FROM `table` WHERE `reply` = '$b[reply]'");

}

?>

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.