I have a table that keeps record of vote
Condition
Reply can have many votes;
User can vote once to specific reply id.
table: reply_vote
+----+-------------+-------------+
| id | reply_id | userID |
+----+-------------+----+--------+
| 1 | 23 | 21 |
| 3 | 33 | 21 |
| 4 | 23 | 25 |
| 5 | 23 | 12 |
I want to display the total votes on each reply
e.g
reply_id 23 has 3 votes with userid (21,25,12)
reply_id 33 has 1 vote with userid (21)
I don't want to fetch all data using while loop in php
up till now I have used below query but it is displaying all the votes on the page which is not what is required
SELECT
reply.id,
reply.reply,
COUNT(reply_vote.id) AS likes
FROM reply
LEFT JOIN reply_vote ON reply_vote.reply_id = reply.id
GROUP BY reply.id
How can I do that?
Try this code:
SELECT reply.id, reply.reply,
COUNT(reply_vote.id) as likes
FROM reply
INNER JOIN reply_vote ON reply_vote.reply_id = reply.id
GROUP BY reply_vote.reply_id
Related
there are two tables , blog and comments table. in blog table all blogs are stored an
Blog Table
----------------------------------------
id | blog_name | date
----------------------------------------
1 | abc | 31/3/17
----------------------------------------
2 | xyz | 31/3/17
----------------------------------------
3 | rbc | 31/3/17
----------------------------------------
4 | ert | 31/3/17
----------------------------------------
Comment Table
----------------------------------------
id | comment | blog_id | approved
----------------------------------------
1 | abc | 1 | 0
----------------------------------------
2 | xyz | 1 | 1
----------------------------------------
3 | rbc | 2 | 0
----------------------------------------
4 | ert | 4 | 1
----------------------------------------
now i want to show results the way .. that all blogs will show but with the count of comments with each blog.. but the comment count should be only for approved comments.. by default the approved comment is 0 means not approved but 1 means approved..the problem is if i join the two tables in where condition i write approved=1 then it shows only 2 blogs but i need to show all blogs with comments.. if any blog comment is not present in the comment table then it will show 0 count to me... please help me with this
im using this join.. this join not showing all blogs..
SELECT b.id,b.blog_name,COUNT(c.comment) as comment_count FROM blog b LEFT JOIN comment c ON b.id=c.blog_id WHERE c.approved=1 GROUP BY b.id
i want all blogs with comments.. it shows only approved comments related blogs..
SELECT
blog.*,
COALESCE(COUNT(comments.id),0) AS comment_count
FROM blog
LEFT JOIN comments
ON comments.blog_id = blog.id AND comments.approved = 1
GROUP BY blog.id
I need to develop chat application in php for this i have created two tables likes users and message. every user details will be stored in users table and every message will be stored in messages table.I have done storing part it is working fine. Now i need to display messages.So as per my requirement.
when any user logs into his portal he/she will be able to see latest messaged users list.And if he want to message any of other users ,he just clicks on there profile pic than a message panel will be opened .Untill here i completed everything.
But my issue is i need to display
latest messaged users list,
in this i need to show user first name, profile picture,last message, last message date.
And one more condition is i need to display the list like latest messaged user first.
I have tried in many ways but i got users list with first message i don't want like that i need last message for that user
My tables are
Users table
uid | firstname | email | mobile
---------------------------------------------
1 | kumar | kumar#gmail.com | 9876543210
----------------------------------------------
2 | jack | jack#gmail.com | 8876543216
----------------------------------------------
3 | rams | rams#gmail.com | 7876543215
----------------------------------------------
4 | devid | devid#gmail.com | 9876543220
----------------------------------------------
5 | joe | joe#gmail.com | 8876543212
----------------------------------------------
messages table
mid| from_id | to_id | message | created_at
----------------------------------------------------------------
1 | 1 | 2 | hello jack | 2017-02-03 09:00:52
----------------------------------------------------------------
2 | 2 | 1 | hi kumar | 2017-02-03 09:10:30
----------------------------------------------------------------
3 | 2 | 3 | ram where are you | 2017-02-03 09:15:02
----------------------------------------------------------------
4 | 3 | 2 | at home | 2017-02-03 09:35:20
----------------------------------------------------------------
5 | 1 | 2 | hello how are you | 2017-02-03 09:42:55
----------------------------------------------------------------
6 | 4 | 2 | good morning | 2017-02-03 09:50:45
----------------------------------------------------------------
8 | 1 | 3 | hi | 2017-02-03 09:54:22
----------------------------------------------------------------
7 | 3 | 1 | hello kumar | 2017-02-03 09:58:38
----------------------------------------------------------------
For example i have logged in as kumar(uid=1)
Expected output:
firstname | message | mid | uid
-----------------------------------------
rams | hello kumar | 7 | 3
-----------------------------------------
jack | hello how are you | 5 | 2
-----------------------------------------
I have tried like this :
SELECT DISTINCT
`u`.`firstname`,
`u`.`profile_photo`,
`u`.`uid`,
`u2`.`firstname`,
`u2`.`profile_photo`,
`u2`.`uid`,
`message`,
`messages`.`created_at`,
`messages`.`from_id`,
`messages`.`to_id`,
`messages`.`mid`
FROM
`messages`
INNER JOIN
`users` AS `u` ON `u`.`uid` = `messages`.`from_id`
INNER JOIN
`users` AS `u2` ON `u2`.`uid` = `messages`.`to_id`
WHERE
(from_id = 1 OR to_id = 1)
GROUP BY
`u`.`uid`,
`u2`.`uid`
ORDER BY
`messages`.`mid` DESC
But got output like this
firstname | message | mid | uid
-----------------------------------------
jack | hello jack | 1 | 2
-----------------------------------------
rams | hi | 5 | 2
-----------------------------------------
Thanks in advance
try this way
SELECT DISTINCT `u`.`firstname`,`u`.`profile_photo`, `u`.`uid`, `u2`.`firstname`,`u2`.`profile_photo`,`u2`.`uid`, `message`,`messages`.`created_at`, `messages`.`from_id`,`messages`.`to_id`,`messages`.`mid`
FROM `messages`
INNER JOIN `users` AS `u` ON `u`.`uid` = `messages`.`from_id`
INNER JOIN `users` AS `u2` ON `u2`.`uid` = `messages`.`to_id`
WHERE (from_id = 1 OR to_id = 1)
GROUP BY `u`.`uid`, `u2`.`uid`
ORDER BY `messages`.`created_at` DESC
It appears that you want to put messages in the same group if they're between the same two users, regardless of the direction. To do this, change your GROUP BY to:
GROUP BY GREATEST(u.uid, u2.uid), LEAST(u.uid, u2.uid)
Use this along with the solutions in SQL Select only rows with Max Value on a Column to get the first or last message in each conversation found using this grouping.
You should also give aliases to the columns from u and u2 in the SELECT clause, so you can distinguish the sender and receiver information in the result.
SELECT u.firstname AS sender_name, u.profile_photo AS sender_photo, ...
Or since one of the users is always kumar, you could just select only the information about the other user:
SELECT IF(from_id = 1, u2.firstname, u1.firstname) AS firstname,
IF(from_id = 1, u2.profile_photo, u1.profile_photo) AS profile_photo,
...
I have three tables:
// Posts
+----+----------+---------------+-----------+
| id | title | content | id_author |
+----+----------+---------------+-----------+
| 1 | title1 | content1 | 1234 |
| 2 | title2 | content2 | 5678 |
+----+----------+---------------+-----------+
// Users
+----+--------+--------+
| id | name | active |
+----+--------+--------+
| 1 | jack | 1 |
| 2 | peter | 0 |
| 3 | John | 1 |
+----+--------+--------+
// Votes
+----+---------+---------+
| id | id_post | id_user |
+----+---------+---------+
| 1 | 32 | 1234 |
| 2 | 634 | 5678 |
| 3 | 352 | 1234 |
+----+---------+---------+
Now I need to check two conditions before inserting a new vote into Votes table:
The id of author and what I have passed are the same? Posts.id_user = :author (I know I can do that by a FK, but I don't want)
The account of current user is active? Users.active = 1
Also here is my query:
INSERT INTO Votes (id_post,id_user)
SELECT ?,?
FROM Posts p
WHERE p.id_user = :author limit 1;
How can I add second condition Users.active = 1 to my query?
EDIT: Actually I'm trying to don't let people be able to vote who are inactive (active = 0). For example if SO bans men, then I cannot vote to post anymore, because I (current user) am banned. So I'm pretty sure $_SESSION['id'] should be used in the query.
INSERT INTO Votes (id_post,id_user)
SELECT p.id,u.id
FROM Posts p, Users u
WHERE p.id_user = :author
AND u.id = :user
AND u.active = 1 limit 1;
then you set parameter user equal to the current user id.
EDIT: I suppose id_user in table Votes must be the voter's id, not the author of the post (correct?), so I fixed the query eliminating the JOIN.
Use and with where
INSERT INTO Votes (id_post,id_user)
SELECT ?,?
FROM Posts p, Users u
WHERE p.id_user = :author and u.active = 1 limit 1;
INSERT INTO Votes (id_post,id_user)
SELECT p.id, u.id
FROM Posts p
INNER JOIN Users u ON 1 = 1
WHERE p.id_user = :author
AND u.id = :current_user_id
AND u.active = 1
LIMIT 1;
I have a table that contains users where they have set a minium age and maxium age they want to be shown to.
Table looks like this:
=========================================
| id | username | age | minAge | maxAge |
=========================================
| 1 | Clark | 20 | 16 | 50 |
-----------------------------------------
| 2 | Kent | 33 | 22 | 25 |
-----------------------------------------
| 3 | Bruce | 45 | 18 | 25 |
-----------------------------------------
| 4 | Wayne | 40 | 23 | 45 |
-----------------------------------------
If 'Clark' is logged in, he will see: 'Bruce'.
If 'Kent' is logged in, he will see: 'Clark', 'Wayne'.
If 'Bruce' is logged in, he will see: 'Clark', 'Wayne'.
If 'Wayne' is logged in, he will see: 'Clark'.
No one will see 'Kent', because no one is in his visible range.
How would something like this work?
I have looked at something like this:
SELECT * FROM table_user WHERE age BETWEEN '16' AND '50';
But this of course is the other way around.
This is a join on the age column, using between (or similar logic). The challenge is getting the tables in the right order:
select u.username, group_concat(tosee.user_name) as users_seen
from table_user u left join
table_user tosee
on tosee.age between u.minage and u.maxage and
tosee.username <> u.username
group by u.username;
In my messages table I have following rows for example,
|----|---------|--------------|------|
| id | user_id | message |status|
|====|=========|==============|======|
| 1 | 2 | msgs 11 | r |
|----|---------|--------------|------|
| 2 | 3 | msgs 12 | r |
|----|---------|--------------|------|
| 3 | 2 | msgs 13 | r |
|----|---------|--------------|------|
| 4 | 3 | msgs 14 | u |
|----|---------|--------------|------|
Now, I need to know two things for each user_id
Whether it has any status u or not.
How many messages are there
For example, a query like below
select user_id, status, count(*) as totalMsg from messages group by user_id
Would brought me following rows
| user_id | status| totalMsg |
|=========|=======|==========|
| 2 | r | 2 |
|---------|-------|----------|
| 3 | r | 2 |
^
|------> I need this value to be 'u' because user 3 has a message u
My current query doesnt really gurantee that it will look for a u in the status column.
Is that possible to do? If so how?
MAX() will work on this since r is the least value based on the lexicographical order.
SELECT user_ID,
MAX(status) status,
COUNT(*) totalMsg
FROM messages
GROUP BY user_ID