我想在从 table 执行 select 到 table_n 时获取 return 计数行

i want get return count rows when execute select into table_n from table

当我运行

select * into mobile_n from mobile where c_name='dic'

我想得到select count(1) from mobile_n

的结果

我试过了

select count(1) 
from ( 
  select * into mobile_n from mobile where c_name='dic' 
  return * 
)

但是没用

您可以尝试使用 CTE 作为 count 结果

WITH result AS (
    select * 
    into mobile_n 
    from mobile 
    where c_name='dic' 
    RETURNING 1
)
SELECT count(*) 
FROM result;

你不能。 https://www.postgresql.org/docs/current/sql-selectinto.html.

SELECT INTO creates a new table and fills it with data computed by a query. The data is not returned to the client, as it is with a normal SELECT. The new table's columns have the names and data types associated with the output columns of the SELECT.

emphasis/bold 我的

解决方法:

create table mobile_n as select  * from mobile limit  0;
with a as(
insert into  mobile_n select  * from mobile where c_name = 'dic' returning 1)
select count(*) from a;