SQL为一列加入多个条件

时间:2017-03-23 17:02:24

标签: mysql sql join

在我的情况下,我有两张表,一张包含所有机构资料的表格和一张代表每个机构所研究资料的表格。

在搜索中,我想查找研究具体材料类型的机构名称。因此,有可能寻找研究“木材”和“金属”,只是“金属”等的机构......

我尝试过以下查询:

    SELECT p.name
    FROM q1_institution_profiles p
    INNER JOIN q9_materials_studied_by_institution pf 
    ON pf.id_institution = p.id_institution
    WHERE pf.id_material_studied = 10 AND pf.id_material_studied = 8

但是结果是空的,因为当我有多个条件时出现问题(这里id_material_studied必须等于8和10)。

有没有办法正确实现这一目标,还是我必须为每个标准加入?

提前感谢您的回答

1 个答案:

答案 0 :(得分:1)

我认为这就是你想要的:

Intent intent = new Intent(mContext, RequestActivityService.class);
PendingIntent callbackIntent = PendingIntent.getService(mContext, 111,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);
ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(googleApiClient,5000, callbackIntent);

public class RequestActivityService extends IntentService {
public RequestActivityService() {
    super("RequestActivityService");
}

@Override
protected void onHandleIntent(Intent intent) {

        if (ActivityRecognitionResult.hasResult(intent)) {

            ActivityRecognitionResult result = ActivityRecognitionResult
                    .extractResult(intent);

            DetectedActivity mostProbableActivity = result
                    .getMostProbableActivity();

            int confidence = mostProbableActivity.getConfidence();

            int activityType = mostProbableActivity.getType();
            String activityName = getNameFromType(activityType);

        }
}

即,获取具有两个值中每个值的行的名称。

Awareness.SnapshotApi.getDetectedActivity(googleApiClient). setResultCallback(new ResultCallback<DetectedActivityResult>() { @Override public void onResult(@NonNull DetectedActivityResult detectedActivityResult) { if (!detectedActivityResult.getStatus().isSuccess()) { Log.i("Could not get the activity",""); return; } ActivityRecognitionResult ar = detectedActivityResult.getActivityRecognitionResult(); DetectedActivity probableActivity = ar.getMostProbableActivity(); String activityType = RequestActivityService.getNameFromType(probableActivity.getType()); int activityConfidence = probableActivity.getConfidence(); } }); 子句仅选择材料8和10. SELECT p.name FROM q1_institution_profiles p INNER JOIN q9_materials_studied_by_institution pf ON pf.id_institution = p.id_institution WHERE pf.id_material_studied IN (8, 10) GROUP BY p.name HAVING COUNT(DISTINCT pf.id_material_studied) = 2; 然后按WHERE对这些材料进行分组。问题是:单个名称是否这两种这些材料?嗯,这就是GROUP BY所做的。