mysql - INSERT INTO and the subquery with COUNT -


i'd put photo data table articles_photos , condition numbers of photos selected article.

both tables exist. below have presented query

insert articles_photos(title,                              filename,                              photo_order,                              created,                              article_id)        values ('title',                'filename',                 (select count(id)                    articles_photos                   article_id = 7) + 1,                now(),                7) 

phpmyadmin says:

static analysis:  5 errors found during analysis.      comma or closing bracket expected (near "select" @ position 109)     unrecognized keyword. (near "count" @ position 116)     unexpected token. (near "(" @ position 121)     unexpected token. (near "id" @ position 122)     unexpected token. (near ")" @ position 124)  #1093 - table 'articles_photos' specified twice, both target 'insert' , separate source data 

what did wrong?

you close. believe following work:

insert articles_photos(title,                          filename,                          photo_order,                          created,                          article_id)  select 'title',      'filename',      count(id)+1,      now(),      7 articles_photos  article_id = 7; 

you should able select same table upon inserting, can't in subquery inside values list had in question. instead, here, move constants down select statement.


Comments