如何在mysql中获取列名

时间:2016-07-05 05:49:40

标签: mysql sql

这是phpmyadmin表:user_test_detail this is phpmyadmin table : user_test_detail

我想要下面的结果

(
[1] => option_a
[2] => option_d
[3] => option_e
[4] => option_d
)

2 个答案:

答案 0 :(得分:1)

获取IT表单信息架构

package pdfshare.hemanthreddy.com.pdfshare.activities;

import android.graphics.Color;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View; 
import android.widget.Toast;

import com.roughike.bottombar.BottomBar;
import com.roughike.bottombar.BottomBarBadge;
import com.roughike.bottombar.BottomBarTab;
 import com.roughike.bottombar.OnMenuTabSelectedListener;
import com.roughike.bottombar.OnTabSelectedListener;

import pdfshare.hemanthreddy.com.pdfshare.R;
import pdfshare.hemanthreddy.com.pdfshare.fragments.GroupsFragment;
import pdfshare.hemanthreddy.com.pdfshare.fragments.HomeFragment;
 import pdfshare.hemanthreddy.com.pdfshare.fragments.NotificationsFragment;
import pdfshare.hemanthreddy.com.pdfshare.fragments.ProfileFragment;

 public class HomeScreen extends AppCompatActivity {

ViewPager pager;
BottomBar bottomBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_screen);
    bottomBar = BottomBar.attach(this,savedInstanceState);
    pager = (ViewPager) findViewById(R.id.viewpagerhome);
    MyPagerAdapter obj = new MyPagerAdapter(getSupportFragmentManager());
    //the following two if statements are used to check if objects are null 
    if(obj.getCount()==4)
        Log.e("obj","not null");
    if(pager == null)
        Log.e("pager","null");
    pager.setAdapter(obj);
     bottomBar.setItems(new BottomBarTab(R.mipmap.ic_action_home_24,"home"),
            new BottomBarTab(R.mipmap.ic_action_user_group,"groups"),
            new   BottomBarTab(R.mipmap.ic_action_notification,"notifications"),
            new BottomBarTab(R.mipmap.ic_action_profile,"profile")
           );
    bottomBar.setOnItemSelectedListener(new OnTabSelectedListener() {
        @Override
        public void onItemSelected(int position) {
               T oast.makeText(getApplicationContext(),position,Toast.LENGTH_LONG).show();
            pager.setCurrentItem(position);
        }
    });



    BottomBarBadge message = bottomBar.makeBadgeForTabAt(2,"red",10);
    message.show();

}



private class MyPagerAdapter extends FragmentStatePagerAdapter
{

    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position)
        {
            case 0:
                return new HomeFragment();
            case 1:
                return new GroupsFragment();
            case 2:
                return new NotificationsFragment();
            case 3:
                return new ProfileFragment();
            default:
                return new HomeFragment();
        }
    }

    @Override
    public int getCount() {
        return 4;
    }
}


}

答案 1 :(得分:1)

试试这个。

select 
    case
        when option_a = '1' then `option_a`
        when option_b = '1' then `option_b`
        when option_c = '1' then `option_c`
        when option_d = '1' then `option_d`
        when option_e = '1' then `option_e`
    end as colName
from user_test_detail
order by question_id

这仅适用于表user_test_detail中的这5列,如果将来更改此表的结构,则必须使用动态sql。

<强> 编辑:

select 
    max(case when question_id = 1 then colName end) as `1`,
    max(case when question_id = 2 then colName end) as `2`,
    max(case when question_id = 3 then colName end) as `3`,
    max(case when question_id = 4 then colName end) as `4`
from (
    select 
        case
            when option_a = '1' then `option_a`
            when option_b = '1' then `option_b`
            when option_c = '1' then `option_c`
            when option_d = '1' then `option_d`
            when option_e = '1' then `option_e`
        end as colName,
        question_id
    from user_test_detail
    order by question_id
) t
-- group by question_id