How to select all columns, and a count(*) in the same query

后端 未结 2 964
北荒
北荒 2020-12-28 13:23

In often use in TSQL the following query :

SELECT COUNT(*), * 
FROM CUSTOMER c 
WHERE c.Name like \'foo%\';

When I try to execute this que

2条回答
  •  暖寄归人
    2020-12-28 14:23

    One approach is to do something like the following. This will result in a count(*) result for each line. But beware, there is a Cartesianjoin; if you have many rows like 'foo%' this will perform badly.

    select a.cntr, c.*
    from CUSTOMER c 
       , (select count(*) cntr
         from customer b
         where b.name like 'foo%' ) a
    where c.name like 'foo%'
    

提交回复
热议问题