Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如果计数(id)在表中有多个,则更新列_Mysql_Sql_Sql Server - Fatal编程技术网

Mysql 如果计数(id)在表中有多个,则更新列

Mysql 如果计数(id)在表中有多个,则更新列,mysql,sql,sql-server,Mysql,Sql,Sql Server,我有一张像这样的桌子 ID CustId CustName Status 1 a1 A NULL 2 a1 A NULL 3 a2 B NULL 4 a3 B NULL 5 a4 C NULL 6 a4 C NULL 7 a5 D NULL 8 a6 E NULL 我想

我有一张像这样的桌子

ID  CustId  CustName Status
1     a1       A     NULL
2     a1       A     NULL
3     a2       B     NULL
4     a3       B     NULL
5     a4       C     NULL
6     a4       C     NULL
7     a5       D     NULL
8     a6       E     NULL
我想在
count(custid)>1时更新
status=2
,在
count(custid)=1时更新
status=1
我希望输出如下

ID  CustId  CustName Status
1     a1       A     1
2     a1       A     2
3     a2       B     1
4     a3       B     1
5     a4       C     1
6     a4       C     2
7     a4       D     2
8     a6       E     1
使用联接更新:

它将在sql server中工作:

update tablename set status=cstatus
from tablename inner join
(
select custid, count(custid) as cstatus
from tablename)b on tablename.custid=b.custid
在Mysql中,以下内容将起作用

UPDATE tablename
    JOIN (
    select custid, count(custid) as cstatus
    from tablename)b on tablename.custid=b.custid
SET set status=cstatus
使用联接更新:

它将在sql server中工作:

update tablename set status=cstatus
from tablename inner join
(
select custid, count(custid) as cstatus
from tablename)b on tablename.custid=b.custid
在Mysql中,以下内容将起作用

UPDATE tablename
    JOIN (
    select custid, count(custid) as cstatus
    from tablename)b on tablename.custid=b.custid
SET set status=cstatus
试试这个:

declare @tbl table (ID int,  CustId char(2), CustName char(1), Status int);
insert into @tbl values
(1,'a1','A',NULL),
(2,'a1','A',NULL),
(3,'a2','B',NULL),
(4,'a3','B',NULL),
(5,'a4','C',NULL),
(6,'a4','C',NULL),
(7,'a5','D',NULL),
(8,'a6','E',NULL);

update t1 set t1.Status = case when t2.st > 1 then 2 else 1 end
from @tbl t1
join (select ID, COUNT(*) over (partition by custname) st from @tbl) t2
on t1.ID = t2.ID

select * from @tbl
试试这个:

declare @tbl table (ID int,  CustId char(2), CustName char(1), Status int);
insert into @tbl values
(1,'a1','A',NULL),
(2,'a1','A',NULL),
(3,'a2','B',NULL),
(4,'a3','B',NULL),
(5,'a4','C',NULL),
(6,'a4','C',NULL),
(7,'a5','D',NULL),
(8,'a6','E',NULL);

update t1 set t1.Status = case when t2.st > 1 then 2 else 1 end
from @tbl t1
join (select ID, COUNT(*) over (partition by custname) st from @tbl) t2
on t1.ID = t2.ID

select * from @tbl
用这个

SELECT tbl.id
      ,tbl.cust_id
      ,tbl.cust_name
      ,(SELECT COUNT(cust_id)
        FROM Mytable
        WHERE cust_id = tbl.cust_id) AS status
FROM MyTable tbl;
-- to update table
-- first create a dummy table
create table tbl as select tbl.id, tbl.cust_id, tbl.cust_name, (select count(cust_id) from Mytable where cust_id = tbl.cust_id) as status from MyTable tbl;
-- drop original
drop table mytable;
-- rename dummy
alter table tbl rename to mytable;
用这个

SELECT tbl.id
      ,tbl.cust_id
      ,tbl.cust_name
      ,(SELECT COUNT(cust_id)
        FROM Mytable
        WHERE cust_id = tbl.cust_id) AS status
FROM MyTable tbl;
-- to update table
-- first create a dummy table
create table tbl as select tbl.id, tbl.cust_id, tbl.cust_name, (select count(cust_id) from Mytable where cust_id = tbl.cust_id) as status from MyTable tbl;
-- drop original
drop table mytable;
-- rename dummy
alter table tbl rename to mytable;

您使用的是mysql还是sql server?请不要标记未涉及的产品。发布您的查询如何正确显示您尝试的查询和标记问题。您使用的是mysql还是sql server?请不要标记未涉及的产品。发布您的查询如何正确显示您尝试的查询和标记问题。