Mysql o仅当所有具有fkId=5000的行都具有r状态时,才使其成功。@bobobo。我明白你在说什么。您希望不存在而不是存在。 if (select count(*) from Users where fkId=5000 and status='r') =

Mysql o仅当所有具有fkId=5000的行都具有r状态时,才使其成功。@bobobo。我明白你在说什么。您希望不存在而不是存在。 if (select count(*) from Users where fkId=5000 and status='r') = ,mysql,sql,Mysql,Sql,o仅当所有具有fkId=5000的行都具有r状态时,才使其成功。@bobobo。我明白你在说什么。您希望不存在而不是存在。 if (select count(*) from Users where fkId=5000 and status='r') = (select count(*) from Users where fkId=5000) then .. how many rows with id=5000 and status = 'r'? how many rows with id


o仅当所有具有
fkId=5000
的行都具有
r
状态时,才使其成功。@bobobo。我明白你在说什么。您希望
不存在
而不是
存在
if (select count(*) from Users where fkId=5000 and status='r') =
   (select count(*) from Users where fkId=5000) then ..
how many rows with id=5000 and status = 'r'?
how many rows with id=5000?
are those numbers equal? then ..
     SELECT count(*)>0 FROM Users WHERE fkId = 5000 AND status != 'r'
if not exists (select 1
               from users
               where fkid = 5000 and (status <> 'r' or status is null)
              )
SELECT IF(1, 'Yes', 'No') AS yesorno
SELECT IF (
   (SELECT COUNT(*) FROM Users WHERE fkId=5000 AND status IN('r') AND status NOT IN('1', 'a', 'k')) = (SELECT COUNT(*) FROM Users WHERE fkId=5000),
   'They are equal.',
   'They are not equal.'
)
AS are_they_equal
CREATE TABLE Users(id int NOT NULL AUTO_INCREMENT, fkID int NOT NULL,  status char(1), PRIMARY KEY (id));

INSERT Users (fkID, status) VALUES (5000, 'r');
INSERT Users (fkID, status) VALUES (5000, 'r');
INSERT Users (fkID, status) VALUES (5000, 'r');

-- The next query produces "0" to indicate no miss-matches
SELECT COUNT(*) FROM Users u1 LEFT JOIN Users u2 ON u1.id=u2.id AND u2.status='r' WHERE u1.fkID=5000 AND u2.id IS NULL;

-- now change one record to create a miss-match
UPDATE Users SET status='l' WHERE id=3 ;

-- The next query produces "1" to indicate 1 miss-match
SELECT COUNT(*) FROM Users u1 LEFT JOIN Users u2 ON u1.id=u2.id AND u2.status='r' WHERE u1.fkID=5000 AND u2.id IS NULL;

DROP TABLE Users;
IF EXISTS 
   ( SELECT 1 
     FROM Users 
     WHERE fkId = 5000 
     HAVING MIN(status) = 'r' 
        AND MAX(status) = 'r'
   )