mysql插入记录,如果有什么>别的

时间:2012-10-29 21:33:20

标签: mysql sql

我有一个查询

$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

1 个答案:

答案 0 :(得分:1)

只需使用select代替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
    );";