T
T
Taras Serevann2016-03-10 17:01:09
PHP
Taras Serevann, 2016-03-10 17:01:09

How to implement this in SQL?

There is the following table structure:
7010f93258fb4c64b7e086df30fbf075.png
There is the following code:

/ позбатомися о том, чтобы не было не было ответов с заминусованым рейтингом
    $answers = $wpdb->get_results("SELECT DISTINCT `expert_id` FROM texperts_answers", ARRAY_A);
 
    // пройдем по каждому ответу
    foreach($answers as $i => $answer)
    {
        // получим его рейтинг
        $rating = $wpdb->get_var("SELECT SUM(`rating`) FROM texperts_answers WHERE `expert_id` = " . $answer['expert_id']);
 
        // если рейтинг меньше 0, то делаем его раным 0
        if ($rating < 0) {
            $wpdb->query("DELETE FROM texperts_answers WHERE `expert_id` =  ". $answer['expert_id']);
        }
    }

How to implement the same, but within the framework of SQL, without using PHP?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2016-03-10
@Taras_Serevann

Something like this:

delete from texperts_answers 
where `expert_id` in (SELECT `expert_id` FROM texperts_answers 
group by `expert_id`
having SUM(`rating`)<0)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question