简体   繁体   中英

PHP MYSQL Query to combine and add two equalent result

Somebody please help me to solve my sql query i have already spend two days for this....

i have a MYSQL query given below

(SELECT
  c.cl_list as cl_list,
  c.name as name,
  pc.value as value,
  count( pc.value) as total
FROM
  projs p 
  LEFT JOIN classify_proj_new pc 
    ON p.proj_id = pc.proj_id_fk
  LEFT JOIN classify_list c 
    ON c.cl_list = pc.class_id_fk
WHERE
  MATCH ( p.title ) AGAINST ( 'jerm'  IN BOOLEAN MODE )
GROUP BY 
  c.cl_list,
  pc.value)
UNION ALL
(SELECT
  c.cl_list as cl_list,
  c.name as name,
  pc.value as value,
  count( pc.value) as total
FROM
  jerm p 
  LEFT JOIN classify_jerm_new pc
    ON p.jerm_id = pc.jerm_id_fk
  LEFT JOIN classify_list c
    ON c.cl_list = pc.class_id_fk
WHERE
  MATCH ( p.jermname ) AGAINST ( 'jerm'  IN BOOLEAN MODE )
GROUP BY
  c.cl_list,
  pc.value)

Which Gives a result of (below):

  cl_list      name              value        total
------------------------------------------------------------------------------------
    1       department         jewller          2
    3       price                 50            2
    6       color                blue           1
    6       color                Red            2
    1       department         jewller          1
    6       color                Red            1

but i am trying to get a result which can add the repeating value's total and avoid repeating value....some thing like this (below):

  cl_list      name              value        total
------------------------------------------------------------------------------------
    1       department         jewller          3
    3       price                 50            2
    6       color                blue           1
    6       color                Red            3

somebody please help me i am very sad about my output...

Thank you very much in advance...

Select from your query and group by cl_list, name and value:

SELECT
  cl_list,
  name,
  value,
  sum(total) as total
FROM (
  -- your current query here ...
) data
GROUP BY
  cl_list,
  name,
  value

try below code. it will display unique record and avoid duplication.

GROUP BY c.name

group the result on name and value

(SELECT c.cl_list as cl_list, c.name as name, pc.value as value, count( pc.value) as total
FROM projs p 
LEFT JOIN classify_proj_new pc ON p.proj_id = pc.proj_id_fk
LEFT JOIN classify_list c ON c.cl_list = pc.class_id_fk
WHERE MATCH ( p.title ) AGAINST ( 'jerm'  IN BOOLEAN MODE )
GROUP BY c.name, pc.value)
UNION ALL
(SELECT  c.cl_list as cl_list, c.name as name, pc.value as value, count( pc.value) as total
 FROM jerm p 
 LEFT JOIN classify_jerm_new pc ON p.jerm_id = pc.jerm_id_fk
 LEFT JOIN classify_list c ON c.cl_list = pc.class_id_fk
 WHERE MATCH ( p.jermname ) AGAINST ( 'jerm'  IN BOOLEAN MODE )
 GROUP BY c.name, pc.value)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM