0

我有一个查询

$query = "INSERT INTO `user`
    (`fname`,
    `lname`,
    `email`,
    `password`,
    `permission`,
    `idcustomer`)
         VALUES
    (
    '" . $firstname . "',
    '" . $lastname . "',
    '" . $email . "',            
    '" . $password . "',
    'admin',
    " . $idcustomer . "
    );";

现在,我只需要执行此查询,以下查询的结果是 > 0:

select (number_of_users - (select count(*) from user where idcustomer=2)) availableUsers 
   from customer where idcustomer=2
4

1 回答 1

1

只需使用 aselect而不是values(...)

$query = "INSERT INTO `user`
    (`fname`,
    `lname`,
    `email`,
    `password`,
    `permission`,
    `idcustomer`)
     select
    '" . $firstname . "',
    '" . $lastname . "',
    '" . $email . "',            
    '" . $password . "',
    'admin',
    " . $idcustomer . "
    from customer
    where idcustomer = 2
          and (number_of_users - (select count(*) from user where idcustomer=2)) > 0
    );";
于 2012-10-29T21:45:00.910 回答