从同一表创建MySQL JOIN(parent_id中的parent_title)

时间:2018-07-13 20:05:13

标签: mysql sql

我想知道实现以下结果的最简单方法是什么?

table_companies

package mediaser.tivvmialive;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.TextView;
import android.net.Uri;
import android.widget.MediaController;
import android.widget.VideoView;

public class MainActivity extends AppCompatActivity {

    private TextView mTextMessage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTextMessage = (TextView) findViewById(R.id.message);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        VideoView videoView =(VideoView)findViewById(R.id.videoView2);
        MediaController mediaController= new MediaController(this);
        mediaController.setAnchorView(videoView);
        Uri uri=Uri.parse("http://rumblehwk.altervista.org/VideoHome/default.mp4");
        videoView.setMediaController(mediaController);
        videoView.setVideoURI(uri);
        videoView.requestFocus();
        videoView.start();
    }

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    Intent myIntent = new Intent(MainActivity.this, MainActivity.class);
                    startActivity(myIntent);
                    break;
                case R.id.navigation_dashboard:
                    Intent myIntent2 = new Intent(MainActivity.this, Archivio.class);
                    startActivity(myIntent2);
                    break;
                case R.id.navigation_notifications:

                    break;
            }
            return true;
        }
    };

}

结果:

co_id | co_parent_id | co_title
------|--------------|----------
1       0              name1
2       1              name2
3       1              name3

更新:我知道应该使用LEFT JOIN来实现,但老实说,我已经筋疲力尽,很长时间没有播放过查询了...

非常感谢您的帮助。

解决方案(由D-Shih和Florian提供)

//左加入

Parent_Title    Title
(none)          name1
name1           name2
name1           name3 

//右加入

SELECT t2.co_title AS Parent_Title, t1.co_title AS Title 
    FROM table_companies t1 
    LEFT JOIN table_companies t2 ON t1.co_parent_id = t2.co_id

2 个答案:

答案 0 :(得分:2)

您需要自我RIGHT JOIN

SELECT t2.co_title 'Title',t1.co_title 'Parent_Title'
FROM T t1 
RIGHT JOIN T t2 on t1.co_id  = t2.co_parent_id  

sqlfidde:http://sqlfiddle.com/#!9/ab8fbf/8

答案 1 :(得分:2)

您必须自己加入表格。试试:

SELECT t2.co_title as Parent_Title, t1.co_title as Title 
FROM table_companies t1 
LEFT JOIN table_companies t2 ON t1.co_parent_id = t2.co_id
相关问题