Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

PHP:Konsep sistem peringkat bintang?

inilah contoh mysql yang sangat sederhana:

drop table if exists image;
create table image
(
image_id int unsigned not null auto_increment primary key,
caption varchar(255) not null,
num_votes int unsigned not null default 0,
total_score int unsigned not null default 0,
rating decimal(8,2) not null default 0
)
engine = innodb;

drop table if exists image_vote;
create table image_vote
(
image_id int unsigned not null,
user_id int unsigned not null,
score tinyint unsigned not null default 0,
primary key (image_id, user_id)
)
engine=innodb;

delimiter #

create trigger image_vote_after_ins_trig after insert on image_vote
for each row
begin
 update image set 
    num_votes = num_votes + 1,
    total_score = total_score + new.score,
    rating = total_score / num_votes  
 where 
    image_id = new.image_id;
end#

delimiter ;

insert into image (caption) values ('image 1'),('image 2'), ('image 3');

insert into image_vote (image_id, user_id, score) values
(1,1,5),(1,2,4),(1,3,3),(1,4,2),(1,5,1),
(2,1,2),(2,2,1),(2,3,4),
(3,1,4),(3,5,2);

select * from image;
select * from image_vote;


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Apakah Urutan Bidang Indeks Multi-Kolom di MySQL Penting?

  2. Bagaimana Mengembalikan Tabel MySQL Tunggal Menggunakan mysqldump?

  3. mysql hapus dalam mode aman

  4. Pada impor MySQL:ERROR pada baris 32769:Perintah tidak dikenal '\''

  5. Memecahkan kegagalan tautan komunikasi dengan JDBC dan MySQL