sql

Posted zjxgdq

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql相关的知识,希望对你有一定的参考价值。

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demotest.dao.VideoMapper">
    <sql id="sele_fi">
        name
        ,phone,cove
    </sql>
    <select id="selectById" resultType="video">
        select *
        from video
        where id = #video_id;
    </select>
    <select id="selectByIdList" resultType="video">
        select <include refid="sele_fi"></include>
        from video;
    </select>
    <insert id="add" parameterType="video" useGeneratedKeys="true" keyColumn="id"
            keyProperty="id">
        insert into video (name, phone, cove)
        values (#name, #phone, #cove)
    </insert>
    <insert id="addHandle" parameterType="video">
        insert into video (name, phone, cove)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (#item.name, #item.phone, #item.cove)
        </foreach>
    </insert>
    <update id="editUpdate" parameterType="video">
        update video
        <trim prefix="set" suffixOverrides=",">
            <if test="name !=null">
                name = #name,
            </if>
            <if test="phone !=null">
                phone = #phone,
            </if>
            <if test="cove !=null">
                cove = #cove,
            </if>
        </trim>
        where id = #id
    </update>
    <delete id="deleteId" parameterType="video">
        delete
        from video
        where id = #id
    </delete>
</mapper>

  

sql sql.sql

-- pripravime si temporary tabulku pro manipulaci s cenami, dovolime NULL ID (jinak to cele nebude fungovat)
DROP TEMPORARY TABLE IF EXISTS credit_voucher;

CREATE TEMPORARY TABLE credit_voucher LIKE `pricelist`;
CREATE TEMPORARY TABLE credit_backup_top LIKE `pricelist`;

ALTER TABLE credit_voucher
  DROP KEY `PRIMARY`,
  CHANGE `id` `id` INT(11) UNSIGNED NULL DEFAULT NULL;

ALTER TABLE credit_backup_top
  DROP KEY `PRIMARY`,
  CHANGE `id` `id` INT(11) UNSIGNED NULL DEFAULT NULL;

-- vezmeme si aktualni ceny pro 150GB pro CZ i SK, celkem 18
INSERT INTO credit_voucher
  SELECT *
  FROM pricelist
  WHERE
    valid_to IS NULL
    AND `data_amount` = 150000000
    AND `sms_number` IS NULL
    AND country IN ('CZ', 'SK'); -- 9x od kazde;


INSERT INTO credit_backup_top
  SELECT *
  FROM pricelist
  WHERE
    valid_to IS NULL
    AND `data_amount` = 70000000
    AND `sms_number` IS NULL
    AND is_highlighted = 1
    AND country IN ('CZ', 'SK'); -- 9x od kazde;


BEGIN;
-- deaktivujeme puvodni ceny
UPDATE `pricelist`
SET `valid_to` = NOW()
WHERE `id` IN (
  SELECT `id`
  FROM credit_voucher
);

-- pripravime si kopii puvodnich cen, ktere budou platne od noveho roku
UPDATE credit_voucher
SET id = NULL, archived_id = NULL, valid_to = NULL, valid_from = '2017-01-01 00:00:00';

-- nasypeme pripravene nove ceny do tabulky
INSERT INTO `pricelist`
  SELECT *
  FROM credit_voucher;

-- pripravime cepickovou polozku
UPDATE credit_voucher
SET valid_from = NOW(), valid_to = '2017-01-01 00:00:00', is_highlighted = 1;

-- zlavime to nam
UPDATE credit_voucher
SET `price` = 18
WHERE `country` = 'SK';

-- aj bratom Čechom
UPDATE credit_voucher
SET `price` = 399
WHERE `country` = 'CZ';


-- nasypeme pripravene nove ceny do tabulky
INSERT INTO `pricelist`
  SELECT *
  FROM credit_voucher;




-- vytvoreni top polozek pro 70GB od ledna a deaktivace top polozek pro obdobi NOW() - novy rok

-- deaktivujeme puvodni ceny
UPDATE `pricelist`
SET `valid_to` = NOW()
WHERE `id` IN (
  SELECT `id`
  FROM credit_backup_top
);

-- pripravime si kopii puvodnich cen, ktere budou platne od noveho roku
UPDATE credit_backup_top
SET id = NULL, archived_id = NULL, valid_to = NULL, valid_from = '2017-01-01 00:00:00';

-- nasypeme pripravene nove ceny do tabulky
INSERT INTO `pricelist`
  SELECT *
  FROM credit_backup_top;


-- nasypeme pripravene nove ceny do tabulky
INSERT INTO `pricelist`
  SELECT *
  FROM credit_backup_top;


-- udelame z top 70GB polozek obycejne polozky
UPDATE credit_backup_top
SET valid_from = NOW(), valid_to = '2017-01-01 00:00:00', is_highlighted = 0;


-- nasypeme pripravene nove ceny do tabulky
INSERT INTO `pricelist`
  SELECT *
  FROM credit_backup_top;

-- vsechno se povedlo :)
COMMIT;

-- uklid
DROP TEMPORARY TABLE credit_voucher;

以上是关于sql的主要内容,如果未能解决你的问题,请参考以下文章

sql [sql技巧]一些sql技巧#sql

sql 2008 r2 在sql 2008上兼容么

SQL基础

有大神知道,sql server 中如何批量执行sql语句吗?

pl sql developer怎么执行sql

pl sql developer怎么执行sql