还是上午那道题目,只是有了些变动,要加上where条件,就有了变化
比如:
SELECT p.*,count( d.product_id ) AS total FROM product p left join dig_product d on p.product_id = d.product_id where d.votetime>=1178812800 and d.votetime<=1178899199 group by p.product_id order by total desc,p.product_time desc 
如果不加where d.votetime>=1178812800 and d.votetime<=1178899199的话,就会检索出正常值,但是如果加上的话,那么就会只检索出dig_product中的不重复的产品所以还需要大家帮忙了原帖连接:
http://community.csdn.net/Expert/topic/5539/5539101.xml?temp=.9018824题目叙述:表一:product
字段如下:
id      product_id      product_time(上市时间)
1       1               2007-12-01
2       2               2007-11-01
3       3               2007-10-01
4       4               2007-09-01
5       5               2007-08-01
6       6               2007-07-01表二:dig_product 
字段如下:
id      product_id    addtime(添加评论时间)
1       1             2007-05-17
2       1             2007-05-17
3       1             2007-05-17
4       3             2007-05-17
5       3             2007-05-17
6       4             2007-05-17
7       5             2007-05-17这两个表的含义是:product表是一个产品表,dig_product是关于产品的评论表,把每天评论的机型存放到这个表中。也就是说每个产品在这个表中有多条纪录,比如:在2007-05-17这一天里,product_id等于1的产品被评论了3次,在这个表的体现形式就是存在3条product_id等于1的,addtime等于2007-05-17的纪录。现在查询的要求是:
两个表联合查询,把所有机型取出来做个排序,按照评论数最多,上市时间最新的来排序。
比如:把每个产品按照评论的次数的多少来排序,比如有1个10次的,5个9次的,那么就是10次的排在最前面,5个9次的再按照上市时间这样来排,没有被评论的产品就是评论次数为0次,也一次按照上市时间来排序。下面看看我写的:
SELECT p.*,count( d.product_id ) AS total
FROM product p left join dig_product d on p.product_id = d.product_id  group by d.product_id order by total desc,p.product_time desc这样在排序方面能够做到先按照评论次数来排序,然后再按照上市时间来排序,但是现在有一个问题是:检索出来的纪录数目有问题。dig_product表中有多少条纪录,就只能检索出多少条纪录来。
这不符合我的要求,我的要求是把所有产品都检索出来,没有被评论的就是0次,按照上市时间来排序。

解决方案 »

  1.   

    我写了一个,是这样的
    SELECT p.*,count( d.product_id ) AS total FROM product p left join dig_product d on p.product_id = d.product_id where d.votetime>=1178812800 and d.votetime<=1178899199  group by p.product_id,p.product_time
    union
    select *,0 as total from product
    但是有个问题,虽然全部纪录是正确的,但是检索出来的数据并没有排序了
    因为一加上order by p.product_id desc,p.product_time desc
    就会提示Wrong usage of UNION and ORDER BY
      

  2.   

    select * from (
    SELECT p.*,count( d.product_id ) AS total FROM product p left join dig_product d on p.product_id = d.product_id where d.votetime>=1178812800 and d.votetime<=1178899199  group by p.product_id,p.product_time
    union
    select *,0 as total from product)t
    order by t.product_id desc,t.product_time desc
      

  3.   

    我写了一个,是这样的
    SELECT p.*,count( d.product_id ) AS total FROM product p left join dig_product d on p.product_id = d.product_id where d.votetime>=1178812800 and d.votetime<=1178899199  group by p.product_id,p.product_time
    union
    select *,0 as total from product
    但是有个问题,虽然全部纪录是正确的,但是检索出来的数据并没有排序了
    因为一加上order by p.product_id desc,p.product_time desc
    就会提示Wrong usage of UNION and ORDER BY-------------------------------------------------------
    select * from 
    (
    SELECT p.*,count( d.product_id ) AS total FROM product p left join dig_product d on p.product_id = d.product_id where d.votetime>=1178812800 and d.votetime<=1178899199  group by p.product_id,p.product_time
    union
    select *,0 as total from product
    ) a 
    再在后面加 order by
      

  4.   

    gahade(与君共勉) 虽然这样可以排序了,但是检索出来的数据比正常得到的要多
      

  5.   

    select * from (
    SELECT p.*,count( d.product_id ) AS total FROM product p left join dig_product d on p.product_id = d.product_id where d.votetime>=1178812800 and d.votetime<=1178899199  group by p.product_id,p.product_time
    union
    select *,0 as total from product)t
    order by t.product_id desc,t.product_time desc这句把dig_product中的不重复的产品和product中所有产品检索出来了
    有部分是重复的
    应该怎么在union
    select *,0 as total from product)t
    order by t.product_id desc,t.product_time desc
    给消除重复的呢?
      

  6.   

    --改為union all 試試
    select * from (
    SELECT p.*,count( d.product_id ) AS total FROM product p left join dig_product d on p.product_id = d.product_id where d.votetime>=1178812800 and d.votetime<=1178899199  group by p.product_id,p.product_time
    union all --改為union all
    select *,0 as total from product)t
    order by t.product_id desc,t.product_time desc