mySQL内部加入两个相同的外键

时间:2016-01-15 14:31:28

标签: php mysql database inner-join

我有一张桌子

Question
->id
->question_name
->columnheader

Mutual
-id
->question_id_1
->question_id_2

基本上我需要从Mutual表格中制作一份记录列表,但我不能只显示question_id_1question_id_2所以我需要进行内部联接。问题是我有question_id_1question_id_2作为我的外键。

如果我的结构就像

Mutual
->id
->question_id

然后我可以简单地

Select b.id, a.question_name FROM question a INNER JOIN mutual b ON a.id = b.question_id

如何在我当前的数据库结构中执行此操作?我想在问题表

中显示question_id_1the question_name`

2 个答案:

答案 0 :(得分:2)

我认为你需要这样的东西:

     $("#canvas-holder").droppable({    
     accept: '.cmp',
     tolerance: 'pointer',
     drop: function(event, ui) {
             $('#save_button').prop("disabled", false);
             x = ui.helper.clone();
             $(this).append(x.attr('id',generateUniqueID(x)).addClass("resizable ui-resizable"));
             $("#canvas-holder .cmp").addClass("item");
             $(".item").removeClass("ui-draggable cmp");


             current_component_height = $("#canvas-holder").height();
             current_component_width = $("#canvas-holder").width();
             existingElements[$(x).attr("id")] = [current_component_height,current_component_width] 
             alert(JSON.stringify(existingElements))

             $(".item").css({
                 "left": $("#canvas-holder").offset().left+"px",
                 "top": $("#canvas-holder").offset().top+"px",
                 "height": ($("#canvas-holder").height())/4+"px",
                 "width": $("#canvas-holder").width()+"px"
             });


             $(".item").resizable({resize: function(event, ui) {
                                    $("form input[name=width]").removeAttr('max')
                                    $("form input[name=height]").removeAttr('max')
                                     restrictSize(ui);
                                     console.log("before: "+$(this).height())
                                     var height = $(this).height() * 5.0;
                                    console.log("after: "+height)
                                     if($(this).height() <= canvasHeight){
                                       $("form input[name=height]").val(height)
                                     }else{
                                       var rem=(canvasHeight - $(this).height())*5
                                       height= Math.round(height + rem); 
                                       $("form input[name=height]").val(height)
                                     }

                                     var width = $(this).width() * 5.0;
                                     if($(this).width() <= canvasWidth){
                                         $("form input[name=width]").val(width)
                                     }else{
                                       var rem=(canvasWidth - $(this).width())*5
                                       width= Math.round(width + rem); 
                                       $("form input[name=width]").val(width)
                                     }
                                     }
                         });

             $(".item").draggable({
                 containment: '#canvas-holder',
                 drag: function(){
                     console.log($(this).scrollTop())
                     $("form input[name=width]").removeAttr('max')
                     $("form input[name=height]").removeAttr('max')
                     var offset = $(this).offset();
                     if($(this).hasClass('selected')){
                        var comp_ypoint = offset.top - canvas_ypoint;
                        var comp_xpoint = offset.left - canvas_xpoint;
                        comp_xpoint = comp_xpoint * 5.0
                        comp_ypoint = comp_ypoint * 5.0

                        if(comp_xpoint>=0)
                             $("form #x-cord-id").val(comp_xpoint)

                        if(comp_ypoint>=0)
                            $("form #y-cord-id").val(comp_ypoint)
                     }
             }
             });

答案 1 :(得分:0)

我真的希望看到预期的最终结果,但你可以像这样使用UNION项目:

SELECT q.id, question_name FROM mutual m 
INNER JOIN question q 
ON q.id = m.question_id_1 

UNION

SELECT q.id, question_name FROM mutual m 
INNER JOIN question q 
ON q.id = m.question_id_2