MySQL double left join, condition OR and group

Pionas

i have got some problem. I try count all articles in categories and subcategories.

I use this query:

SELECT 
    cat.*, COUNT(art.id) AS total
FROM
    article_categories cat
        LEFT JOIN
    article_categories c2 ON (c2.category_parent = cat.category_id)
        LEFT JOIN
    articles art ON (art.cid = cat.category_id
        OR art.cid = c2.category_id)
WHERE
    cat.category_parent = 0 AND art.st = 1
        AND IF(art.cid IN (2 , 4, 16, 17, 18, 19, 20, 21, 22),
        TO_DAYS(CURDATE()) - TO_DAYS(art.date_edit) < 120,
        art.cid)
GROUP BY IF(c2.category_parent > 0,
    c2.category_parent,
    cat.category_id)
ORDER BY cat.category_order ASC

but for some category total = 36355

when i use this query:

SELECT 
    *
FROM
    `articles`
WHERE
    st = 1
        AND cid IN (2 , 4, 16, 17, 18, 19, 20, 21, 22)
        AND IF(cid IN (2 , 4, 16, 17, 18, 19, 20, 21, 22),
        TO_DAYS(CURDATE()) - TO_DAYS(date_edit) < 120,
        cid)

total = 7730

What am I doing wrong?

My tables:

articles:
- id
- cid
- title
- date_add
- date_edit
- st

article_categories
- category_id
- category_name
- category_order
- category_parent

Thanks for help

[EDIT]

$sql1 = "SELECT cat.*, COUNT(art.id) AS total FROM article_categories cat LEFT JOIN articles art ON (art.cid = cat.category_id) WHERE cat.category_parent = 0 AND IF(art.cid IN (2 , 4, 16, 17, 18, 19, 20, 21, 22), TO_DAYS(CURDATE()) - TO_DAYS(art.date_edit) < 120, art.cid) GROUP BY art.cid";

$category = array();
foreach ($row as $cat) {
    $total = $cat['total'];
    $sql2 = "SELECT cat.*, COUNT(art.id) AS total FROM article_categories cat LEFT JOIN articles art ON (art.cid = cat.category_id) WHERE cat.category_parent = ".$cat['category_id']." AND IF(art.cid IN (2 , 4, 16, 17, 18, 19, 20, 21, 22), TO_DAYS(CURDATE()) - TO_DAYS(art.date_edit) < 120, art.cid) GROUP BY art.cid";
    foreach ($row2 as $cat2) {
        $total += $cat2['total'];
    }
    $category[] = array('category_id'=> $cat['category_id'], 'total' => $total);
}
Pionas

I created another table: article_categories_rel.

article_categories_rel
- child_id
- parent_id

My own new query:

SELECT
    cat.*,
    COUNT(art.id) as ilosc
FROM
    article_categories cat
LEFT JOIN
    article_categories_rel acr
    ON
        (cat.category_id=acr.parent_id)
LEFT JOIN
    articles art
    ON (
        art.cid = acr.child_id AND
        art.st=1 AND
        if(art.cid in(2,4,16,17,18,19,20,21,22),
        TO_DAYS( curdate( ) ) - TO_DAYS( art.date_edit )<120,
        art.cid)
    )
WHERE
    cat.category_parent=0
GROUP BY
    cat.category_id
ORDER BY
    cat.category_order ASC;

and it works ;)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySQL LEFT JOIN with OR condition

From Dev

MySQL left join and group by

From Dev

MySQL left join count with condition

From Dev

MYSQL LEFT JOIN and COUNT and GROUP BY

From Dev

mysql Left Join, group by with count

From Dev

GROUP_CONCAT with LEFT JOIN condition?

From Dev

MySQL: Left Join RIGHT JOIN GROUP BY Error

From Dev

Mysql with Group by + IF + Left Join + Group_Concat

From Dev

MySQL Query - How do I get a SUM with GROUP BY and WHERE condition and use LEFT OUTER JOIN?

From Dev

Mysql double left outer join with count

From Dev

MySQL case statement with left join condition

From Dev

MySQL left join with additional like condition

From Dev

mysql LEFT JOIN with condition from other table

From Dev

Mysql left join with condition in right table

From Dev

Mysql LEFT JOIN same table with WHERE condition

From Dev

How to use if condition with left join in mysql

From Dev

Mysql LEFT JOIN same table with WHERE condition

From Dev

Mysql select count on left join with condition not working

From Dev

Select rows with Left Outer Join and condition - MySQL

From Dev

Mysql left join with condition in right table

From Dev

MySQL Left Join With And Condition To Same Column

From Dev

SUM mixed with LEFT JOIN and GROUP BY in MYSQL query

From Dev

MySQL - Left Join + Group by not working as expected

From Dev

Left Join or Group By for Finding Maximum Salary in Mysql

From Dev

How to do LEFT JOIN with double condition in Oracle syntax?

From Dev

mysql left join and group by not returning all results in left table

From Dev

Double results in GROUP_CONCAT with double JOIN in MySQL

From Dev

LEFT JOIN with OR Condition

From Dev

Condition left join in CriteriaQuery

Related Related

HotTag

Archive