常用sql

﹥>﹥吖頭↗ 提交于 2020-08-05 01:51:24

1. 建表语句:


CREATE TABLE `user2` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) DEFAULT NULL,
  `sex` varchar(5) DEFAULT NULL,
  `address` varchar(100) DEFAULT NULL,
  `birthday` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8

2. 需求:有两张人员表,需要将这两张表中数据拼接在一起(实现这个表中没有在另一张表中查)

 <select id="selectAll" parameterType="java.util.Map" resultType="com.smxny.parking.entity.VehicleInformation">
    SELECT
    pi.user_name ownerName,
    pi.mobel ownerTelephone,
    vi.id id,
    vi.owner_id ownerId,
    vi.vehicle_brand vehicleBrand,
    vi.vehicle_color vehicleColor,
    vi.vehicle_no vehicleNo,
    vi.vehicle_type vehicleType
    FROM
    parking_space.vehicle_information vi
    INNER JOIN   household.household_info  pi
    ON pi."id" = vi.owner_id
    WHERE 1=1
    <if test="ownerName!=null and ownerName!=''">
      and pi.user_name = #{ownerName}
    </if>
    <if test="telephone!=null and telephone!=''">
      and pi.mobel = #{telephone}
    </if>
    <if test="carNumber!=null and carNumber!=''">
      and vi.vehicle_no= #{carNumber}
    </if>
    UNION
    SELECT
    o.owner_name ownerName,
    o.owner_phone ownerTelephone,
    vi.id id,
    vi.owner_id ownerId,
    vi.vehicle_brand vehicleBrand,
    vi.vehicle_color vehicleColor,
    vi.vehicle_no vehicleNo,
    vi.vehicle_type vehicleType
    FROM
    parking_space.owner_information o
    INNER JOIN parking_space.vehicle_information vi
    ON vi.owner_id = o.id
    WHERE 1=1
    <if test="ownerName!=null and ownerName!=''">
      and o.user_name = #{ownerName}
    </if>
    <if test="telephone!=null and telephone!=''">
      and o.mobel = #{telephone}
    </if>
    <if test="carNumber!=null and carNumber!=''">
      and vi.vehicle_no= #{carNumber}
    </if>
  </select>

 

 

 

3. mybatis中判断字符串是否为空

    <if test="null != name and !name.isEmpty()">
      and name like concat('%', #{name}, '%')
    </if>

4. mybatis中模糊查询

and name like concat('%', #{name}, '%')

5. sql中判读集合的大小

        <if test="userNameIdList!=null and (userNameIdList.size()>0)">
            AND store_user_id in
            <foreach collection="userNameIdList" index="index" item="item" open="("
                     separator="," close=")">
                #{item}
            </foreach>
        </if>

 

 

6.  sql中数据类型转换

cast(build_code as int)

7. foreach中使用switch

 <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>

8. 字段添加默认值

ALTER TABLE asset_account ALTER COLUMN asset_balance SET DEFAULT 0.00;

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!