MySQL查询无法使用嵌套选择

时间:2014-04-28 19:51:16

标签: php mysql pdo

以下代码似乎不起作用,我不是MySQL的专家,但这似乎应该有用,任何线索?

if($params['action'] == 'createTab'){
    $stmt = $dbh->prepare("
        insert into
            dashboard_data
        (position, user_id)
            values
        (   
            (select
                max(position) + 1
            from
                dashboard_data
            where
                user_id=:userid
            ),
            :userid
        )
    ");
    $stmt->bindParam(':userid', $userId);
    $stmt->execute();

1 个答案:

答案 0 :(得分:1)

使用

insert into dashboard_data (position, user_id)
select max(position) + 1, :userid 
from dashboard_data 
where user_id = :userid