2012-04-29 67 views
2

我已经USERS表包含以下字段_idnamenumber_of_posts如何根据另一个表中的值更新数据库记录?

和我有POSTS TABLE包含以下字段_iduser_idpost_text

没有usersposts之间的关系是one- to -many即,一个用户有许多后

问题是如何更新number_of_postsusers台,使得其将持有posts

UPDATE

后的号码,我有这些表充满了大约上百条记录就没有办法使用触发器来更新number_of_posts。

+0

+1好日常问题 – 2012-04-29 14:44:37

+0

如何?我是新手 – user4o01 2012-04-29 14:48:45

回答

1

你可以使用posts表上的触发器来做到这一点。每次插入帖子时,都要更新users表中的number_of_posts。

http://dev.mysql.com/doc/refman/5.6/en/triggers.html

更新:充分合作解决方案

drop table if exists users; 
create table users ( 
    _id bigint unsigned auto_increment primary key, 
    name varchar(50) not null, 
    number_of_posts integer not null default 0 
); 

drop table if exists posts; 
create table posts ( 
    _id bigint unsigned auto_increment primary key, 
    user_id bigint unsigned not null, 
    post_text text 
); 

-- Populate with dummy data 
insert into users (name) values ('Bob'), ('Sally'); 
insert into posts (user_id, post_text) 
    values (1, 'First!!'), (1, 'Second...'), 
      (2, 'Post 1'), (2, 'another post'), (2, 'more posts'); 

-- One-time update of users table 
update users u 
set u.number_of_posts = (  
    select count(0) from posts p 
    where u._id = p.user_id  
); 

-- trigger to keep post count up to date when future posts are made 
drop trigger if exists trg_post_count; 
delimiter $$ 

create trigger trg_post_count 
after insert on posts 
for each row 
begin 
    select count(0) into @post_count 
    from posts 
    where user_id = NEW.user_id; 

    update users 
    set number_of_posts = @post_count 
    where _id = NEW.user_id; 
end; 
$$ 

delimiter ; 

-- test trigger 
insert into posts (user_id) values (2); 
select * from users; 
+0

+1在MySQL中练习使用触发器 – 2012-04-29 14:44:08

+0

查看我的更新 – user4o01 2012-04-29 14:46:16

+0

count(0)是做什么的? – user4o01 2012-04-29 16:23:48

1

作为一个时间更新,试试这个,但是触发的解决方案是要保持它当前是正确的:

UPDATE users AS u SET u.number_of posts = (SELECT COUNT(*) FROM posts AS p WHERE p.user_id=u.user_id) 
+0

+1为漂亮的解决方案 – user4o01 2012-04-29 15:05:58

0

正如dwurf所建议的那样,我肯定会推荐将其置于触发器中以保持该字段最新。但是,我完全不同意他说,它应该只是一个你在每次更新时增加的数字。这是脆弱的和容易出错的。你应该使用下面的查询找出每个用户到底有多少职位有:

update 
    users 
set 
    number_of_posts = ( 
    select 
     count(0) 
    from 
     posts p 
    where 
     p.user_id = users.id 
    ) 
; 
+1

同意不增加,我已经更新了我的帖子 – dwurf 2012-04-29 15:16:51

2

下面给出的示例脚本演示如何使用LEFT OUTER JOIN加入两个usersposts表来获取与用户相关联的帖子的数量和然后在用户表中的* _id *字段上使用GROUP BY来获取帖子数。

Click here to view the sample in SQL Fiddle.

查询:

CREATE TABLE users 
(
    _id  INT NOT NULL 
    , name VARCHAR(30) NOT NULL 
); 

CREATE TABLE posts 
(
    _id  INT NOT NULL 
    , _userid INT NOT NULL 
    , name VARCHAR(30) NOT NULL 
); 

INSERT INTO users (_id, name) VALUES 
    (1, 'user 1'), 
    (2, 'user 2'), 
    (3, 'user 3'); 

INSERT INTO posts (_id, _userid, name) VALUES 
    (1, 1, 'post 1'), 
    (2, 1, 'post 2'), 
    (3, 2, 'post 3'); 

SELECT   u._id 
      , u.name 
      , COUNT(p._id) number_of_posts 
FROM   users u 
LEFT OUTER JOIN posts p 
ON    u._id = p._userid 
GROUP BY  u._id; 

输出

_ID NAME NUMBER_OF_POSTS 
--- ------ --------------- 
1 user 1   2 
2 user 2   1 
3 user 3   0 
+0

+1 – user4o01 2012-04-29 15:05:51

相关问题