0

我有点混淆将结果放入两个表中,我知道这很简单,但没有任何线索.. 这是我的查询,它给了我结果,但是当我使用时,group by它只是给了我结果一行甚至数据库中的实际结果是 (300, 500) 因为我正在通过lead_id两个表中常见的结果来获取结果..

SELECT inv.amount, crmd.computers, crmd.product_id, crmd.pc_opti
FROM invoice AS inv, lead_crm_data AS crmd
WHERE inv.lead_id = '310'
AND crmd.lead_id = '310'
GROUP BY inv.lead_id

这是给定查询的结果..

amount  computers   product_id  pc_opti
300     2           7, 6            2
300     3           7, 6            3
540     2           7, 6            2
540     3           7, 6            3

结果与GROUP BY

300     2           7, 6            2

期望的结果

300     2           7, 6            2
540     3           7, 6            3
4

1 回答 1

1

试试这个:

SELECT inv.amount, crmd.computers, crmd.product_id, crmd.pc_opti
FROM invoice AS inv, lead_crm_data AS crmd
WHERE inv.lead_id = '310'
  AND crmd.lead_id = '310'
GROUP BY inv.amount

您可以使用更好的(恕我直言)语法:

SELECT inv.amount, crmd.computers, crmd.product_id, crmd.pc_opti
FROM invoice AS inv INNER JOIN lead_crm_data AS crmd 
    ON inv.lead_id = crmd.lead_id 
   AND inv.lead_id = '310'
GROUP BY inv.amount
于 2012-08-20T09:06:45.517 回答