如何使用针对Mysql中第二个表中的值的附加查询设置true / false值?

时间:2015-08-07 07:57:50

标签: mysql

我有2个表,一个用于成员,另一个用于他们的服务。这些是MySQL 5.6服务器上的InnoDB表。

会员表:

package com.example.androidsurfaceviewexample;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.NoSuchElementException;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class CameraActivity extends Activity implements SurfaceHolder.Callback {
    TextView testView;
    Camera camera;
    SurfaceView surfaceView;
    SurfaceHolder surfaceHolder;
    PictureCallback rawCallback;
    ShutterCallback shutterCallback;
    PictureCallback jpegCallback;
    public static boolean front = false;
    Intent intent = null;
    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        intent = getIntent();
        Bundle extras = intent.getExtras();
        front = extras.getBoolean("front");
        surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
        surfaceHolder = surfaceView.getHolder();
        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        surfaceHolder.addCallback(this);
        // deprecated setting, but required on Android versions prior to 3.0
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        jpegCallback = new PictureCallback() {
            public void onPictureTaken(byte[] data, Camera camera) {
                FileOutputStream outStream = null;
                try {
                    String filePath=String.format("/sdcard/%d.jpg", System.currentTimeMillis());
                    outStream = new FileOutputStream(filePath);
                    outStream.write(data);
                    outStream.close();
                    FirstActivity.photo = true;
                    intent = new Intent(getBaseContext(), MainActivity.class);
                    Bundle extras = new Bundle();
                    //extras.putByteArray("path", data);
                     extras.putString("path", filePath);
                    extras.putString("front", "" + front);
                    intent.putExtras(extras);
                    startActivity(intent);
                    Log.d("Log", "onPictureTaken - wrote bytes: " + data.length);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                }
                Toast.makeText(getApplicationContext(), "Picture Saved", 2000)
                        .show();
                refreshCamera();
            }
        };
    }
    public void captureImage(View v) throws IOException {
        // take the picture
        camera.takePicture(null, null, jpegCallback);
    }

    public void refreshCamera() {
        if (surfaceHolder.getSurface() == null) {
            // preview surface does not exist
            return;
        }
        // stop preview before making changes
        try {
            camera.stopPreview();
        } catch (Exception e) {
            // ignore: tried to stop a non-existent preview
        }
        // set preview size and make any resize, rotate or
        // reformatting changes here
        // start preview with new settings
        if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
            camera.setDisplayOrientation(90);
        } else {
            camera.setDisplayOrientation(0);
        }
        try {
            camera.setPreviewDisplay(surfaceHolder);
            camera.startPreview();
        } catch (Exception e) {
        }
    }
    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // Now that the size is known, set up the camera parameters and begin
        // the preview.
        refreshCamera();
    }

    public void surfaceCreated(SurfaceHolder holder) {
        try {
        // open the camera
            System.out.println("camera id"+""+getCamera());
            if(front)
            {
                int frontExist=getCamera();
                if(frontExist==1)
                camera = getFrontFacingCamera();
                else
                    camera = Camera.open();
            }
            else
            camera = Camera.open();
        } catch (RuntimeException e) {
            // check for exceptions
            System.err.println(e);
            return;
        }
        // Camera.Parameters param;
        // param = camera.getParameters();
        // modify parameter
        // param.setPreviewSize(352, 288);
        // camera.setParameters(param);

        try {
            // The Surface has been created, now tell the camera where to draw
            // the preview.
            camera.setPreviewDisplay(surfaceHolder);
            camera.startPreview();
        } catch (Exception e) {
            // check for exceptions
            System.err.println(e);
            return;

        }

    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // stop preview and release camera
        camera.stopPreview();
        camera.release();
        camera = null;
    }

    Camera getFrontFacingCamera() throws NoSuchElementException {
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        for (int cameraIndex = 0; cameraIndex < Camera.getNumberOfCameras(); cameraIndex++) {
            Camera.getCameraInfo(cameraIndex, cameraInfo);
            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                try {
                    return Camera.open(cameraIndex);
                } catch (RuntimeException e) {
                    e.printStackTrace();
                }
            }
        }
        throw new NoSuchElementException("Can't find front camera.");
    }

    int getCamera()
    {
        int f=0;
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        for (int cameraIndex = 0; cameraIndex < Camera.getNumberOfCameras(); cameraIndex++) {
            Camera.getCameraInfo(cameraIndex, cameraInfo);
            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                try {
                    f=1;
                    break;
                } catch (RuntimeException e) {
                    e.printStackTrace();
                }
            }
            else
                f=0;
        }
        return f;
    }

}

服务表:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.androidsurfaceviewexample.CameraActivity" >


    <SurfaceView
        android:id="@+id/surfaceView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.66" />

    <LinearLayout
        android:id="@+id/capture"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:gravity="center"
        android:onClick="captureImage"
        android:orientation="horizontal" >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="10dp"
            android:text="Capture"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </LinearLayout>

</LinearLayout>

我尝试实现这个结果:

id     |       name       |   phone
----------------------------------------
  1       Daniel             123456789
  2       Liam               123456789
  3       Lucas              123456789

结果将取决于members.id = services.MID以及基于其services.category等于'35'的真或假。

1 个答案:

答案 0 :(得分:2)

您可以使用left joincase when

来完成此操作
select
m.id,
m.name,
case when s.MID is not null then 'true' else 'false' end as services
from members m
left join services s on s.MID = m.id and s.category = 35 ;