插入新行时避免数据库重复

时间:2017-09-22 19:36:24

标签: php mysql

您好我已经使用以下内容创建了一个联接表:

CREATE TABLE user_group_join (

    user_group_join_id int(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
    user_join_id int(11) NOT NULL,
    group_join_id int(11) NOT NULL,
    FOREIGN KEY (user_join_id) REFERENCES users (user_id),
    FOREIGN KEY (group_join_id) REFERENCES user_group (group_id)

);

然后我还有另一张桌子:

CREATE TABLE user_flat(

    user_flat_id int(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
    user_flat_user_id INT(11) NOT NULL,
    user_flat_group_id INT(11) NOT NULL,
    user_flat_location varchar(250) NOT NULL,

    FOREIGN KEY (user_flat_user_id) REFERENCES users (user_id),
    FOREIGN KEY (user_flat_group_id) REFERENCES user_group (group_id)

);

user_join_group是管理用户权限,现在每次更新特定用户的单位时,我还需要使用一些唯一值更新user_group_join表,例如,如果我有以下值:

user_join_id | group_join_id
          75 | 12
          75 | 13
          75 | 14

如果我想将group_join_id中的值12更改为13,则应该只有:

   user_join_id | group_join_id
              75 | 13
              75 | 14

我已尝试删除该行,然后插入新行,但问题如下:

如果我的用户id为75,例如组13中有两个单位,组14中有一个单位,如果我用值为12的组更改两个相似单位中的一个,则数据库值将是:

user_join_id | group_join_id
          75 | 12
          75 | 14

当我真的想要回来时:

user_join_id | group_join_id
          75 | 12
          75 | 13
          75 | 14

我希望不要混淆,有人可以帮助我。非常感谢

1 个答案:

答案 0 :(得分:0)

我不确定我是否理解正确,如果我不是,请纠正我。

如果用户可以拥有多个具有相同ID的单位,则应按如下方式设计数据库:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

<div class="pagination">
</div>

<div id="jar" style="display:none">
    <div class="content">1) Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>
    <div class="content">2) Maecenas vitae elit arcu.</div>
    <div class="content">3) Pellentesque sagittis risus ac ante ultricies, ac convallis urna elementum.</div>
    <div class="content">4) Vivamus sodales aliquam massa quis lobortis. </div>
    <div class="content">5) Phasellus id sem sollicitudin lacus condimentum malesuada vel tincidunt neque.</div>
    <div class="content">6) Donec magna leo, rhoncus quis nunc eu, malesuada consectetur orci.</div>
    <div class="content">7) Praesent sollicitudin, quam a ullamcorper pharetra, urna lacus mollis sem, quis semper augue massa ac est.</div>
    <div class="content">8) Etiam leo magna, fermentum quis quam non, aliquam tincidunt erat.</div>
    <div class="content">9) Morbi pellentesque nibh nec nibh posuere, vel tempor magna dignissim.</div>
    <div class="content">10) In maximus fermentum elementum. Vestibulum ac lectus pretium, suscipit ante nec, bibendum erat.</div>
    <div class="content">11) Phasellus sit amet orci at lectus fermentum congue. Etiam faucibus scelerisque purus.</div>
    <div class="content">12) Pellentesque laoreet ipsum ac laoreet consectetur. </div>
    <div class="content">13) Integer aliquet odio magna, lobortis mattis tortor suscipit sed.</div>
    <div class="content">14) Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. </div>
    <div class="content">15) Mauris a tellus luctus turpis elementum imperdiet vitae malesuada mauris. </div>
    <div class="content">16) Donec id libero sagittis, laoreet lorem vel, tempus nunc. </div>
    <div class="content">17) Donec vitae neque sed ex tristique hendrerit.</div>
    <div class="content">18) Aliquam sollicitudin gravida varius.</div>
    <div class="content">19)   Donec auctor, augue sed finibus fermentum, neque erat interdum libero, eget porta metus lectus quis odio.</div>
    <div class="content">20)  Nunc quis ante enim. Etiam nisl orci, hendrerit ut pretium nec, tempor in metus.</div>
    <div class="content">21) Donec et semper arcu.</div>
    <div class="content">22) Donec lobortis interdum purus, eu semper nisl pulvinar ac.</div>
    <div class="content">23) Cras laoreet eu elit vel porta.</div>
    <div class="content">24) Quisque pharetra arcu eget diam posuere commodo.</div>
    <div class="content">25) Nulla ornare eleifend neque, eget tincidunt nunc ullamcorper id. Nulla facilisi.</div>
</div>

然后,在您的查询中,您只需减少计数变量或删除该行(如果它减少到0)。