我需要加入2个表,而只从sales.sales表中提取前10个最常用的sku。
Design.info table:
sku
name
sales.sales table:
basesku
我需要将设计表中的“sku”与sales表中的“basesku”匹配,并在其旁边显示“name”列。 这是我目前从sales.sales表中接收前10个设计的公式:
SELECT basesku, count(*) FROM sales.sales GROUP BY basesku ORDER BY count(*) DESC LIMIT 10
一位同事找到了解决方案:
SELECT sales.sales.basesku, count(*),design.info.name
FROM sales.sales
INNER JOIN design.info
On sales.sales.basesku = design.info.sku
GROUP BY basesku ORDER BY count(*) DESC limit 10
答案 0 :(得分:0)
select basesku,count(*) from sales.sales sale
innner join design.info inf on sale.basesku =inf.sku
group by sale.basesku order by count(*) desc limit 10;
答案 1 :(得分:0)
您可以在WHERE子句中使用SELECT语句。这样的事情应该有效:
SELECT * FROM Design WHERE sku IN
(SELECT basesku, count(*) FROM sales.sales GROUP BY basesku ORDER BY count(*) DESC LIMIT 10)
答案 2 :(得分:0)
我的经验是更多SQL Server,但这可能适用于您想要实现的目标:
SELECT DI.sku, DI.name FROM
Design.info DI
INNER JOIN
(
SELECT basesku, count(*) FROM sales.sales GROUP BY basesku ORDER BY count(*) DESC LIMIT 10
) F ON DI.sku = F.basesku