title: Problem to correctly obtain more solds and visited items in MySQL
body: <p>I'm having trouble getting the total of <strong>most sold</strong> and total <strong>most viewed</strong> products correctly.</p>
  <p>Case best sellers:</p>
  <pre><code>   SELECT p.product_id, p.title, p.description, p.specifications, p.price, p.discount, p.discount_type, p.color, p.hours, p.product_type, p.gender, p.img, p.vdo_intro, p.status, p.url, p.date_start, p.date_end, p.ip, p.product_page, p.product_code, p.brand,   p.color_url,                          
            c.category_id, c.category_url as urlcategory, c.parent,
            SUM(o.order_quantity) as solds
       FROM order_detail o
       JOIN product p ON o.product_id = p.product_id                      
       JOIN category_relations rcat ON rcat.product_id = p.product_id 
  LEFT JOIN `category` c on rcat.category_id = c.category_id  
    WHERE p.gender = ? AND p.status=1 AND p.date_end IS NULL 
   GROUP BY p.product_id
   ORDER BY SUM(o.order_quantity)
       DESC LIMIT 12;
  </code></pre>
  <p>In Data Base i have:</p>
  <pre><code>id_order    order_quantity  order_price order_discount  order_discount_type product_id  order_date
      1           2               59.00        0               %                 1        2022-01-31 22:49:24
      2           5               59.00        0               %                20        2022-01-31 22:49:24
      3          12               59.00        0               %                 8        2022-01-31 22:49:24
      4           5               59.00        0               %                19        2022-01-31 22:49:24
      5          25               59.00        0               %                17        2022-01-31 22:49:24
      6           3               59.00        0               %                 1        2022-01-31 22:49:24
  </code></pre>
  <p><em>Result i get :</em></p>
  <pre><code>id:1    2 + 3 = 5 solds    i get = 20 solds (wrong). 
  id:20   5 solds            i get = 15 solds (wrong).
  id:8   12 solds            i get = 36 solds (wrong).
  id:19   5 solds            i get = 5 solds (ok).
  id:17  25 solds            i get = 50 solds (wrong).
  </code></pre>
  <p>Does anyone know what I'm doing wrong?<br>
  It look like that it is multiplying the results by records.</p>
  <br>
  <p>Case most visits:</p>
  <pre><code>    SELECT p.product_id, p.title, p.description, p.specifications, p.price, p.discount, p.discount_type, p.color, p.hours, p.product_type, p.gender, p.img, p.vdo_intro, p.status, p.url, p.date_start, p.date_end, p.ip, p.product_page, p.product_code, p.brand, p.color_url,                       
             c.category_id, c.category_url as urlcategory, c.parent,
             SUM(v.total) as visits
        FROM `visits` v
        JOIN `product` p ON v.product_id = p.product_id
        JOIN category_relations rcat ON rcat.product_id = p.product_id 
   LEFT JOIN `category` c on rcat.category_id = c.category_id                                     
       WHERE p.gender = ? AND p.status=1 AND p.date_end IS NULL  
    GROUP BY p.product_id
    ORDER BY SUM(v.total)
        DESC LIMIT 12
  </code></pre>
  <p>My database:</p>
  <pre><code>id_visits   total      ip              today        product_id
  23           5     xxxxxxxxxxxxx1   2022-02-09        1
  36           1     xxxxxxxxxxxxx4   2022-02-06       13
  40           1     xxxxxxxxxxxxx3   2022-02-06       13
  41           1     xxxxxxxxxxxxx1   2022-02-06       21
  48           1     xxxxxxxxxxxxx2   2022-02-07       13
  50           1     xxxxxxxxxxxxx2   2022-02-07        1
  62           1     xxxxxxxxxxxxx8   2022-02-08        1
  </code></pre>
  <p><em>Result i get:</em></p>
  <pre><code>id1 :   5 + 1 + 1 = 7 visits      i get 28 visits (wrong)
  id13:   1 + 1 + 1 = 3 visits      i get 9 visits (wrong)
  id21:   1         = 1 visits      i get 3 visits (wrong)
  </code></pre>
  <p>Does anyone know the error, because it does not add up correctly?
  <br></p>
  <blockquote>
  <p>note: I can add the tables if needed</p>
  </blockquote>
  <p><strong>Edit:</strong></p>
  <p><a href="https://www.db-fiddle.com/f/8woBZmnMUpcoauemM1o5nq/1" rel="nofollow noreferrer">DB Fiddle</a></p>
  <p>If i do:</p>
  <p><code>GROUP BY rcat.cat_relation_id</code></p>
  <p>I get the calculation right, but it repeats the same products</p>