使用子查询插入查询

时间:2011-06-17 16:04:43

标签: php mysql sql database

我正在尝试使用子查询进行插入查询。我必须插入除子查询结果之外的其他数据。这是我现在的查询:

INSERT INTO articles (title,content,frontpage,date_created,userID,catID,sectionID) 
values("merijnmoetleren","blalblrsklfdkf", 1, "2010-01-23", 5, 2,
(SELECT id FROM sections WHERE name ="about")

它出了什么问题?

4 个答案:

答案 0 :(得分:5)

看看是否有效

INSERT INTO articles ( title, content, frontpage, date_created, userID, catID, sectionID ) SELECT "merijnmoetleren","blalblrsklfdkf", 1, "2010-01-23", 5, 2, id FROM sections WHERE name ="about"

答案 1 :(得分:5)

试试这个:

INSERT INTO articles
  (title, content, frontpage, date_created, userID, catID, sectionID)
  SELECT "merijnmoetleren", "blalblrsklfdkf", 1, "2010-01-23", 5, 2, id
  FROM sections WHERE name = "about";

答案 2 :(得分:2)

在最后加上另一个关闭的副词。

答案 3 :(得分:0)

INSERT INTO articles ( title
                     , content
                     , frontpage
                     , date_created
                     , userID
                     , catID
                     , sectionID
                     ) 
              values ( "merijnmoetleren"
                     , "blalblrsklfdkf"
                     , 1
                     , "2010-01-23"
                     , 5
                     , 2
                     , (SELECT TOP 1 id FROM sections WHERE name ="about")
                     )