日期:2014-05-16  浏览次数:20893 次

小菜,请教个SQL查询问题
产品表:products
产品订单表:orders_products



产品:products 表结构

products_id products_title 

1 时尚长裙

2 时尚短裙



产品订单:orders_products表结构

orders_id products_id

1 1

2 1

3 2



需求:根据关键字(时尚)查询,并按产品热卖程度排序(统计订单表中产品进行排序)。

请问SQL要怎样写呢,亲们。


------解决方案--------------------
select a.products_id,a.products_title,count(*) as num
from products A inner join orders_products B
on A.products_id = B.products_id where instr(a.products_title,'时尚')>0
group by a.products_id,a.products_title
order by num desc
------解决方案--------------------
SQL code
select p.products_id  ,p.products_title  ,count(*) as num
from products p,orders_products o
where p.products_id=o.products_id
group by p.products_id  ,p.products_title
order by 3 desc