我如何获得具有ID的经理列表?

时间:2016-03-15 17:41:31

标签: sql-server tsql

我有一个带有以下架构的用户表

Select DISTINCT(ManagerId) from Users
group by ManagerId

这个ManagerId只是一个Userid,因为经理也是一个用户。

我可以使用以下查询获取经理列表,但如何获得managerId以及名称?

ManagerId, Name

我想要关注输出

public class CameraFragment extends Fragment {

    public static final int MEDIA_TYPE_IMAGE = 1;
    private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
    private static final String IMAGE_DIRECTORY_NAME = "Myfiles";// directory name to store captured images
    private static String mCurrentPhotoPath;  // file path to the last image captured
    View view;
    private Uri fileUri;// file url to store image
    private GridView myGridView;
    private ImageListAdapter adapter;
    private List<Images> myLists;// image list
    Images images;
    DatabaseAdapter DBadapter;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if (view != null) {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null)
                parent.removeView(view);
        }
        try {
            // Inflate the layout for this fragment
            view = inflater
                    .inflate(R.layout.fragment_camera, container, false);


        } catch (InflateException e) {
            /* map is already there, just return view as it is */
            e.printStackTrace();
        }
        if (view != null)
            init();
        return view;

    }

    // initialize all the variables
    private void init() {
        DatabaseAdapter DBadapter =new DatabaseAdapter(getActivity());
        DBadapter.open();
        myLists = new ArrayList<Images>();
        myLists=DBadapter.getImagesList();
        adapter = new ImageListAdapter(getActivity(), R.layout.img_list_view, myLists);
       // imageView = (ImageView) view.findViewById(R.id.imageListView);
        Button myButton = (Button) view.findViewById(R.id.camerabutton);
        myButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);// create a file to save the image
                intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
                startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE); // start the image capture Intent
            }
        });
        myGridView = (GridView) view.findViewById(R.id.gridView);
        myGridView.setAdapter(adapter);
/**
     * Create a file Uri for saving an image or video
     */
    public Uri getOutputMediaFileUri(int type) {

        return Uri.fromFile(getOutputMediaFile(type));
    }
    /**
     * returning image /
     */
    private static File getOutputMediaFile(int type) {

        // External sdcard location
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), IMAGE_DIRECTORY_NAME);

        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) {
            mediaStorageDir.mkdirs();
            Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
                    + IMAGE_DIRECTORY_NAME + " directory");
            //return null;

        }

        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                Locale.getDefault()).format(new Date());
        File mediaFile;
        if (type == MEDIA_TYPE_IMAGE) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator
                    + "IMG_" + timeStamp + ".jpg");
        } else {
            return null;
        }


        return mediaFile;


    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
                if (resultCode == Activity.RESULT_OK) {
                    Uri selectedImage = data.getData();
                    String[] filePathColumn = {MediaStore.Images.Media.DATA};
                    Cursor cursor = getActivity().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                    cursor.moveToFirst();

                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    //file path of captured image
                    String filePath = cursor.getString(columnIndex);
                    //file path of captured image
                    File f = new File(filePath);
                    String filename = f.getName();
                    cursor.close();
                    DBadapter.insertImage(images);  //insaert image to database 
                    myLists=DBadapter.getImagesList();
                    adapter = new ImageListAdapter(getActivity(), R.layout.img_list_view, myLists);
                    myGridView.setAdapter(adapter);
                    myGridView.invalidateViews();
   break;
 }

            case 2:
        if (resultCode == getActivity().RESULT_CANCELED) {
            // user cancelled Image capture
            Toast.makeText(getActivity(), "User cancelled image capture", Toast.LENGTH_SHORT)
                    .show();
        }
        else {
            // failed to capture image
            Toast.makeText(getActivity(), "Sorry! Failed to capture image", Toast.LENGTH_SHORT)
                    .show();
             } break;
    }}
}

3 个答案:

答案 0 :(得分:3)

试试这个:

SELECT UserId, Name
FROM Permission
WHERE UserId IN (Select DISTINCT ManagerId FROM Permission)

也可以使用JOIN

SELECT UserId, Name
FROM Permission AS p1
JOIN (Select DISTINCT ManagerId FROM Permission) AS p2 
   ON p1.UserId = p2.MAnagerId

答案 1 :(得分:0)

我相信你想要检索作为经理的用户。这可以使用EXISTS子句来完成:

SELECT DISTINCT UserId, Name
FROM Permission p1
WHERE EXISTS (
  SELECT 1
  FROM Permission p2
  WHERE p1.UserId = p2.ManagerId
  )

DISTINCT子句是可选的,根据您的数据可能是无关紧要的。如果是这种情况,请将其删除以提升性能(不同需要对输入进行排序,然后删除重复项)。

答案 2 :(得分:0)

我不确定像Giorgos建议的那样使用INDISTINCT一起使用是个好主意。 SQL Server有一个很好的优化引擎,但我对这些语句的经验不好。

更好地使用EXISTS

SELECT P1.UserId, P1.Name
FROM Permission AS P1
WHERE EXISTS (
        SELECT *
        FROM Permission AS P2
        WHERE P2.ManagerId = P1.UserId
        );

请记住,在Permission表上建立第一个索引列为UserId的索引和另一个索引ManagerId中的索引是个好主意。这种方式EXISTS将非常有效地执行。