所以我得到了以下查询,以便正常工作,搜索' test
*'在' products_desc
'列并在' prices
'中获取所有价格表
SELECT products.id, prices.price, products.product_desc FROM products
INNER JOIN prices
ON prices.product_id = products.id
WHERE
MATCH (products.product_desc) AGAINST ('test*' IN BOOLEAN MODE)
虽然'价格'表格包含每种产品的多个价格,我只想获取每个产品的最低价格。我之前使用
过滤了价格INNER JOIN (
SELECT min(price) as price, prices.product_number as product_number FROM prices
WHERE prices.product_number LIKE'".$q."%'
GROUP BY prices.product_number
) min_prices
on prices.price = min_prices.price
and prices.product_number = min_prices.product_number
但是当我在prices
表中使用products_numbers时(现在只有product_id-column。
Products
id | product_desc
----------------------------------------
1 | product1
2 | product2
Prices
id | product_id | price
------------------------------------------------
1 | 1 | 312
2 | 1 | 219
3 | 2 | 312
4 | 2 | 111
此外,该表包含1000多万行,因此,效率非常重要:)
修改
如果我需要访问min(prices.price)行的列值,该怎么办?
SELECT products.id, MIN(prices.price) as prices_price, prices.id as prices_id, products.product_desc, products.product_number, prices.supplier_id, suppliers.name FROM products
INNER JOIN prices
ON prices.product_id = products.id
INNER JOIN suppliers
ON prices.supplier_id = suppliers.id
WHERE
MATCH (products.product_desc) AGAINST ('test*' IN BOOLEAN MODE)
GROUP BY prices.product_id
上面的产品会返回每个产品的最低价格,但其他列中的错误价值呢?
答案 0 :(得分:1)
GROUP BY 用于对列中的值进行分组,并对列执行计算。
在我们的情况下,我们希望按 product_id 对结果进行分组,因为它在第二个表格中重复并在价格列上执行计算( min())第二张桌子。
这就是您的查询的样子:
SELECT products.id, MIN(prices.price), products.product_desc FROM products
INNER JOIN prices
ON prices.product_id = products.id
WHERE
MATCH (products.product_desc) AGAINST ('test*' IN BOOLEAN MODE)
GROUP BY prices.product_id