PHP, mySql Select all with the same name

advertisements

So I have a table where some of the products repeat with but have a different value on number of clicks.

name ----  clicks
iPhone       4
Samsung      2
iPhone       1
Samsung      5

my select function is :

$select_table2 = 'SELECT * FROM `mytable`'; //WHERE NAME IS THE SAME
$sql2 = $db->query($select_table2);
while ($row = mysqli_fetch_array($sql2)) {
 echo $row["name"];
 echo $row["clicks"];
}

I need this output:

iPhone       5
Samsung      7

I don't want to merge the same rows because they have one more column that is different. So please do not suggest simply to merge them and update the clicks...

UPDATE:

$pull_request = 'SELECT SUM(e.product_clicks),e.product_id, u.name, u.product_id FROM `oc_aa_affiliatecollclicktracking` AS e GROUP BY e.product_id  LEFT JOIN  `'.DB_PREFIX.'product_description` AS u ON e.product_id = u.product_id';

I tried it like this but it's not working


use sum() and also GROUP BY name to get desired output.

$select_table2 = 'SELECT name,SUM(clicks) FROM `mytable` GROUP BY name';
$sql2 = $db->query($select_table2);
while ($row = mysqli_fetch_array($sql2)) {
  echo $row["name"];
  echo $row["clicks"];
}

while will produce,

iPhone       5
Samsung      7

For more info

EDIT

Group by should come after join.

$pull_request = 'SELECT SUM(e.product_clicks) as clicks,e.product_id, u.name, u.product_id FROM `oc_aa_affiliatecollclicktracking` AS e
LEFT JOIN  `'.DB_PREFIX.'product_description` AS u ON e.product_id = u.product_id
GROUP BY e.product_id';