sql="select distinct name,id from products where star='1'"我加上选择出来的记录中就有NAME重复的了呀
同意按鈕JJ, 改SQL好了select distinct id,name from products
那就这个啦: select t.name,products.id from (select distinct name from products) t left join products on t.name=products.name order by products.id
那就这个啦: select t.name,products.id from (select distinct name from products) t left join products on t.name=products.name order by products.id
不好意思,还得加上where stat='1'。这样 select t.name,products.id from (select distinct name from products where star='1') t left join products on t.name=products.name order by products.id desc
我 用sql="select distinct id,name from products where star='1'" 现在程序正常,选择出了三条记录,应该是两条的,我的目的是NAME不重复的用sql="select * from products where star='1' order by sn DESC" 程序出错,主要是因为缺少ID 但是程序测试中选择出了两条正确的记录,那真正正确的语句应该是什么呀!
你用select distinct name from products where star='1'结果有几条?
那你这样吧 select t.name,max(products.id) from (select distinct name from products where star='1') t left join products on t.name=products.name order by products.id desc group by t.name
改SQL好了select distinct id,name from products
select t.name,products.id from (select distinct name from products) t
left join products on t.name=products.name order by products.id
select t.name,products.id from (select distinct name from products) t
left join products on t.name=products.name order by products.id
select t.name,products.id from (select distinct name from products where star='1') t
left join products on t.name=products.name order by products.id desc
现在程序正常,选择出了三条记录,应该是两条的,我的目的是NAME不重复的用sql="select * from products where star='1' order by sn DESC"
程序出错,主要是因为缺少ID
但是程序测试中选择出了两条正确的记录,那真正正确的语句应该是什么呀!
不可能,是不是name中空格的问题,那你在两边加上ltrim(rtrim())啦!
select t.name,max(products.id) from (select distinct name from products where star='1') t
left join products on t.name=products.name order by products.id desc
group by t.name