I want to make a reputation like badges in my project that called senior writer
and junior writer
. User reputation based on the total number like that obtained.
I have 3 table:
Table post:
id_post | news | id_user
3 | IT news | 1
4 | game news | 2
Table user:
id_user | username
1 | dora
2 | boots
3 | swipper
Table vote:
id_vote | id_post | id_user | LIKE
10 | 3 | 2 | 1
11 | 3 | 1 | 1
12 | 4 | 3 | 1
And this is my query:
SELECT p.*, SUM(like) AS like_post,
(SELECT SUM(like) //this is subquery start
FROM user u
LEFT JOIN post p ON p.id_user= u.id_user
LEFT JOIN vote v ON v.id_post=p.id_post GROUP BY u.id) AS reputation // end subquery
FROM post p
LEFT join user u ON p.id_user=u.id_user
LEFT JOIN vote v ON p.id_post=v.id_post
GROUP BY p.id_post
ORDER BY p.id_post DESC
LIMIT 10;
This is my view:
<?php foreach $news as $data:?>
<?php echo $data['id_post'];?>
<?php echo $data['title'];?>
LIKE: <?php echo $data['like_post']; ?>
post by <?php echo $data['username'];?>
reputation: <?php if ($data['reputation']==1)
{echo "JUNIOR writer";}
else
{echo "SENIOR Writer"; }
?>
<?php endforeach;?>
My expectation user Dora will get reputation called senior writer
because receive 2 LIKE. And boots will get reputation called junior writer
because only receive 1 LIKE.
The problem is the return query is an error like this: Subquery returns more than 1 row
Any answer?
Many thanks...
Use this query.
SELECT u.id_user, u.username, SUM(v.like) AS reputation
FROM user u
LEFT JOIN post p ON u.id_user=p.id_user
LEFT JOIN vote v ON v.id_post=p.id_post
GROUP BY u.id_user
Use this php code.
LIKE: <?php echo $data['reputation']; ?>
post by <?php echo $data['username'];?>
reputation: <?php if ($data['reputation']==1)
{echo "JUNIOR writer";}
else
{echo "SENIOR Writer"; }
?>
<?php endforeach;?>
EDIT:**
Query for like_post
SELECT v.id_user,v.id_post,SUM(v.like1) AS like_post
FROM vote v
LEFT JOIN USER u ON v.id_user=u.id_user
GROUP BY u.id_user
Query for reputation
SELECT u.id_user, u.username, SUM(v.like1) AS reputation
FROM USER u
LEFT JOIN post n ON u.id_user=n.id_user
LEFT JOIN vote v ON v.id_post=n.id_post
GROUP BY u.id_user
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.