7

我正在使用MySQL 5.6 版。我有一个event包含两个 id 和一个日期的表,以及一个student包含 column的表id。我想在名称匹配时插入两个 id (WHERE firstname =.., AND lastname=..,)。但是,我不太清楚如何id在一个插入命令中取两行。任何人都可以帮助我吗?

谢谢,保罗

4

1 回答 1

17

If you are trying to insert into a table, then you want to use insert. To get data from another table or query, you would want the insert . . . select form.

From what you say, this seems something like what you want:

insert into event(id1, id2, date)
    select s1.id, s2.id, now()
    from student s1 cross join
         student s2
    where s1.id <> s2.id and
          s1.firstname = firstname and s1.lastname = lastname and
          s2.firstname = firstname and s2.lastname = lastname;

It would return all pairs of students where the names match (but don't have the same id).

于 2013-06-06T14:47:16.813 回答