insert mit where über mehrere zeilen

Einklappen
X
 
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • insert mit where über mehrere zeilen

    hi

    ich möchte bei meinem Forum nur neue Beiträge in ein Forum hinzufügen, wenn das Forum innerhalb einer bestimmten Topicgroup ist.
    Dabei haben die beiden Tabellen folgenden Aufbau (vereinfacht):

    Code:
    create table topicgroups (
    	id int unsigned not null auto_increment primary key,
    	name varchar(32) not null
    );
    create table topics (
    	id int unsigned not null auto_increment primary key,
    	topic_group int unsigned not null,
    	name varchar(32) not null
    );
    create table forum (
    	id int unsigned not null auto_increment primary key,
    	topic_id int unsigned not null,
    	first_entry int unsigned not null
    );
    create table forumentries (
    	id int unsigned not null auto_increment primary key,
    	forum_id int unsigned not null,
    	content text not null
    );
    Ich könnte natürlich vorher abfragen und dann erst entsprechend einfügen. Gibt es da keine Möglichkeit, diese beiden Query in eine insert-Query zu vereinen?
    Ich stelle mir da etwas in folgender Art vor:
    Code:
    insert into forumentries
     set forum_id=$forumid,
     content='$content'
    from forum f
     inner join topics t on( t.id=f.topic_id )
     inner join topicgroups tg on( tg.id=t.topic_group )
    where f.id=$forumid and tg.id=$groupid
    habe es bereit mit einem
    Code:
    insert into ... select ...
    versucht zu adaptieren, allerdings erhalte ich die fehlermeldung
    Code:
    #1136 - Column count doesn't match value count at row 1
    was wohl daran liegt, das ich die id nicht festlege. Macht bei einem auto_increment ja aber auch keinen sinn, da ich die id ja automatisch vergeben lassen will.
    Ich hatte auch schon an einen trigger gedacht, erschien mir jedoch nicht so die beste lösung zu sein.

    Im manual bin ich nicht weiter fündig geworden. Gibt es da etwas, oder muss ich 2 query verwenden bzw. einen trigger definieren?

  • #2
    Kennt ihr das auch, das ihr vor erstellen des Topics stundenlang sucht und kurz nach erstellen des Topics die Lösung findet?

    Code:
    INSERT INTO forumentries( forum_id, content) 
        SELECT $forumid, $content
            FROM forum
                    INNER JOIN topics
                            ON( topics.id=forum.topic_id ) 
                    INNER JOIN topicgroups as tgroups 
                            ON( tgroups.id=topics.topic_group ) 
                    WHERE forum.id=$forumid
                      AND tgroups.group_id=$groupid
    Für alle, die es interessiert!


    Tut mir leid für das Sinnlos-Topic

    Kommentar

    Lädt...
    X