3
SELECT DISTINCT 
           t1.name as t1_name, 
           t1.unit as t1_unit, 
           t1.id_producer_goods AS hi_id_producer_goods, 
          t2.name as t2_name 
FROM Table1 t1 
    left join Table2 t2 on t1.id_web_site=t2.id_web_site 
WHERE t1.id='23'

t1.nameit クエリでカウントを取得するにはどうすればよいですか?

私はコードをチェックします:

SELECT DISTINCT 
           t1.name as t1_name,
           count(t1.name) as count,           
           t1.unit as t1_unit, 
           t1.id_producer_goods AS hi_id_producer_goods, 
           t2.name as t2_name 
FROM Table1 t1 
    left join Table2 t2 on t1.id_web_site=t2.id_web_site 
WHERE t1.id='23'

しかし、コードにはエラーがあります:

Column 'Table1.name' is invalid in the select list because
it is not contained in either an aggregate function or the GROUP BY clause.

私は一列に並んでいないことを知っていcount(t1.name) as count,ますが、どうすれば正しいカウントを取得できますt1.nameか?

PS: query get results with unique rows t1.name, t1.unit, t1.id_producer_goods, t2.name. Countt1.nameは、すべての count 各要素を表示する必要がありますt1.name

4

1 に答える 1

4

追加する必要があります

GROUP BY t1.name,t1.unit,hi.id_producer_goods,t2.name 

他の列で集計関数を使用する場合は常に、残りの列に group by 句を追加する必要があります。

これらについては、個別を削除できます。Group by がそれを行います。

于 2013-10-01T13:47:46.703 に答える