如何在连接两个表时获取范围内的记录

时间:2016-04-15 05:14:50

标签: mysql

对于单个表,我使用此查询获取范围内的记录:

SELECT customerId,firstName,lastName FROM customer  WHERE customerId BETWEEN min AND max; 

但是在加入两个表之后,我怎样才能得到范围内的记录。 在这里,我使用如下的外键,

customer.productId = productDetails.productId

我的查询:

SELECT customer.custId, customer.productId,GROUP_CONCAT(productDetails.productName) as productName, GROUP_CONCAT(productDetails.productPrice) as productPrice FROM customer, productDetails WHERE customer.productId = productDetails.productId  GROUP BY productDetails.productId

1 个答案:

答案 0 :(得分:1)

from what I understand you just need to add an "AND" condition to the query


WHERE customer.productId = productDetails.productId
AND customer.custId BETWEEN min and max; 
GROUP BY productDetails.productId