1. 程式人生 > >Mysql中實現多對多的查詢

Mysql中實現多對多的查詢

1.Mybatis中:

			<if  test="constructionOfficeIdList != null and constructionOfficeIdList.size >0">
				AND (
				<foreach collection="constructionOfficeIdList" item="const" index="index">
					<if test="index ==0">
						 FIND_IN_SET(${const},a.construction_office_ids)
					</if>
					<if test="index >0">
						OR FIND_IN_SET(${const},a.construction_office_ids)
					</if>
				</foreach>
				)
			</if>

2.java後臺中

		if (StringUtils.isNotBlank(transportQuery.getWarningTypeString())) {
			String[] split = transportQuery.getWarningTypeString().split(",");
			for (int i = 0; i < split.length; i++) {
				if (i == 0 && 0 == split.length - 1) {
					sqlMaps.append("and (FIND_IN_SET(" + split[i] + ",a.warning_type))");
				}
				if (i == 0 && 0 != split.length - 1) {
					sqlMaps.append("and (FIND_IN_SET(" + split[i] + ",a.warning_type)");
				}
				if (i != 0 && i != split.length - 1) {
					sqlMaps.append(" or FIND_IN_SET(" + split[i] + ",a.warning_type)");
				}
				if (i != 0 && i == split.length - 1) {
					sqlMaps.append(" or FIND_IN_SET(" + split[i] + ",a.warning_type))");
				}
			}
		}
		transportQuery.setSqlMaps(sqlMaps.toString());