combine two tables in mysql for recent activity functionality - php

In my page I have a tab named Recent activity, In which I have to display the two different types of actions
Recently added choices
Recently voted choices
The table stucture for je_addchoice
je_addpoll table
poll_id | user_id | poll_name | category_id | start_date | end_date
1 | 20 |Naturalflrs| 18 | 2012-12-03 | 2095-12-25
je_addchoice table
choice_id | poll_id | choice_creator_id | choice_name | choice_image | description | ctime
1 | 1 | 20 | Greenish | forest.jpg | forest |135453
je_user_vote table
vote_id | user_id | poll_id | choice_id | datetime_voted | user_type
12 | 31 | 1 | 1 |12-12-2606:23:17| normal
Already I have the result page displays as shown below
The above result is displayed using the query
$result=mysql_query("SELECT * FROM je_addchoice, je_addpoll where je_addpoll.start_date <= '$check_date' AND je_addpoll.end_date >='$check_date' AND je_addpoll.poll_id=je_addchoice.poll_id order by je_addchoice.choicecreationtime desc");
The two tables
1) je_addpoll (Main table for polls)
2) je_addchoice (adding choices for the polls)
But what I want to do here is If any user votes for the poll, It will store into
je_user_vote table as shown above.
I want to display the recently voted choices in the same tab

Try this
use desc order for datetime_voted just before je_addchoice.choicecreationtime desc
like this.
$result=mysql_query("SELECT * FROM (SELECT P.poll_id,P.user_id,P.poll_name,P.category_id,P.start_date,P.end_date,C.choice_id,C.choice_creator_id,C.choice_name,C.choice_image,C.description,C.ctime FROM je_addchoice C, je_addpoll P where P.start_date <= '$check_date' AND P.end_date >='$check_date' AND P.poll_id=C.poll_id order by C.choicecreationtime desc) N,je_user_vote U WHERE U.poll_id=N.poll_id order by U.datetime_voted desc,N.choicecreationtime desc");
OR
$result=mysql_query("SELECT N.poll_id,N.user_id,N.poll_name,N.category_id,N.start_date,N.end_date,N.choice_id,N.choice_creator_id,N.choice_name,N.choice_image,N.description,N.ctime FROM (SELECT P.poll_id,P.user_id,P.poll_name,P.category_id,P.start_date,P.end_date,C.choice_id,C.choice_creator_id,C.choice_name,C.choice_image,C.description,C.ctime FROM je_addchoice C, je_addpoll P where P.start_date <= '$check_date' AND P.end_date >='$check_date' AND P.poll_id=C.poll_id order by C.choicecreationtime desc) N,je_user_vote U WHERE U.poll_id=N.poll_id order by U.datetime_voted desc,N.choicecreationtime desc");

Related

Need help in database query for contest winners

I'm creating a web application where the user will be able to participate in a contest and based on their rank, the user will be rewarded.
(table name contest)
id | contest_name | status
------------------------------------
1 | Test Contest | active
(table participants)
id | user_id | contest_id | score | time_taken
----------------------------------------------------------
1 | 123 | 1 | 10 | 2332 --> in milliseconds
My contest table prize distribution (table name price_distribution)
id | contest_id | rank_start | rank_end | price
-------------------------------------------------------
1 | 1 | 1 | 10 | 50
-------------------------------------------------------
2 | 1 | 11 | 20 | 25
-------------------------------------------------------
Meaning if all the users that rank between 1 to 10, they will get 50 points and rank between 11-20, 20 points so on.
I've used this query to get all the list of users in the contest with their rank.
SELECT participants.score,
participants.time_taken,
contest.name as contest_name,
user.name as username,
user.image as userimage,
FIND_IN_SET( participants.score, (SELECT GROUP_CONCAT( participants.score ORDER BY participants.score DESC, participants.time_taken ASC )
FROM participants )) AS rank
FROM participants
LEFT JOIN contest
ON participants.contest_id = contest.id
LEFT JOIN user ON user.id = participants.user_id
WHERE participants.contest_id = '1'
ORDER BY participants.score DESC, participants.time_taken ASC
LIMIT 50
The above query results is this
score | time_taken | contest_name | username | userimage | rank
-----------------------------------------------------------------------
10 | 2356 | test_contest | abc | image | 1
-----------------------------------------------------------------------
The above query only lists the user based on rank and does nothing else.
I want to reward the user based on rank.. How to achieve this query.
I want to know the query which when executed will reward the user based on their rank and will take the value from the prize distribution table.
Any help will be greatly appreciated.
You can join the table price_distribution based on the user's score:
SELECT participants.score,
participants.time_taken,
contest.name as contest_name,
user.name as username,
user.image as userimage,
FIND_IN_SET( participants.score, (SELECT GROUP_CONCAT( participants.score ORDER BY participants.score DESC, participants.time_taken ASC )
FROM participants )) AS rank,
pd.price
FROM participants
LEFT JOIN contest ON participants.contest_id = contest.id
LEFT JOIN user ON user.id = participants.user_id
LEFT JOIN price_distribution pd
ON participants.contest_id = pd.contest_id and participants.score BETWEEN pd.rank_start AND pd.rank_end
WHERE participants.contest_id = '1'
ORDER BY participants.score DESC, participants.time_taken ASC
LIMIT 50

Ranking SQL query

I'm making kind of top-ten ranking in my app, and I'm stuck in the SQL query that I'll use for that.
I have 2 tables.
The 'posts' table stores the ID of the post autor(user_id), and the post content(and, of course, the entry ID).
+----+---------+--------------+
| ID | user_id | content |
+----+---------+--------------+
| 1 | 3 | Lorem Ipsum1 |
| 2 | 6 | Lorem Ipsum2 |
| 3 | 3 | Lorem Ipsum3 |
+----+---------+--------------+
The 'likes' table, stores ID of the person who liked the post(user_id), the post ID(post_id) and the like date witch is a timestamp(like_date).
+----+---------+---------+------------+
| ID | user_id | post_id | like_date |
+----+---------+---------+------------+
| 1 | 2 | 1 | 1491484851 |
| 2 | 5 | 1 | 1491484871 |
| 3 | 11 | 2 | 1491484891 |
+----+---------+---------+------------+
Every time a user like a post, an entry is created at the 'likes' table, and if the user unlike it, I just remove the entry.
And here's the deal. I want to grab the top 10 most liked users of the last 30 days. I want the query result to be something like this
+---------+-------+
| user_id | likes |
+---------+-------+
| 3 | 2 |
| 6 | 1 |
+---------+-------+
I've already tried a tons of queries and spent a couple of hours trying to solve that, but I just cant figure out how to.
This one should do the trick
select user_id, count(*)
from posts t1
join likes t2
on t1.ID = t2.post_id
where from_unixtime(t2.like_date) >= now() - interval 30 day
group by user_id
order by count(*) desc
limit 10
Assuming you mean "post_id" not "user_id" in your expected results:
SELECT post_id, count(*) as 'likes'
FROM likes
GROUP BY post_id
WHERE like_date >= DATE(NOW() - INTERVAL 30 DAY)
ORDER By 2 desc
LIMIT 10
Everything's in the likes table, so filter recent posts (WHERE), return/order by the count and onl retrieve the first 10 rows. There are other ways to achieve this...
You don't even need a join for this:
SELECT user_id, count(*) as numlikes
FROM likes l
WHERE like_date >= unix_timestamp() - 30*24*60*60
GROUP BY user_id
ORDER BY numlikes desc
LIMIT 10;
The like_date looks like a Unix timestamp, so we should treat it as one. This gets exactly 30 days from the current time to the second. If you want to count days from midnight:
WHERE like_date >= unix_timestamp(CURDATE() - interval 30 day)
EDIT:
If it is the user_id from the post, then you would use join to get that:
SELECT p.user_id, count(*) as numlikes
FROM likes l join
posts p
on l.post_id = p.id
WHERE l.like_date >= unix_timestamp() - 30*24*60*60
GROUP BY p.user_id
ORDER BY numlikes desc
LIMIT 10;

how to write a INNER JOIN on a subquery in PHP

I've a following query to select data from a table (chat) in a chat system:
SELECT * FROM (SELECT * FROM chat WHERE id_chat = '$chat_id' ORDER BY id DESC LIMIT 10) S
WHERE id_chat = '$chat_id'
ORDER BY id ASC
LIMIT 10
In this table (chat), there is a user column where it is the user id that sent the message.
I would, from the id_from of the user who sent the message, get back data that user (users table).
Table chat:
id | id_chat | id_from | id_to | message | date
1 | 23 | 1 | 2 | hello! | 01-12-2016
Table users:
id | name | photo
1 | John | pic.png
2 | Nick | hey.jpg
What better way to do it using the above query? LEFT JOIN? INNER JOIN? And, how do?

Double SQL Query - JOIN

I need a double SELECT sql query from 2 different tables with names visits & items
1.: SELECT visitid, visitdate, visitreason FROM visits WHERE personid = 10
2.: SELECT itemid, itemname, itemtime FROM items WHERE itemvisitid= visitid
I think I need to do a JOIN but don’t know exactly how.
Table examples:
Table: visits
visitid | personid | visitdate | visitreason
1 | 10 | 05/07/2014 | no reason
2 | 10 | 06/07/2014 | some reason
3 | 12 | 06/07/2014 | no reason
4 | 10 | 12/07/2014 | some other reason
Table: items
itemid | personid | itemvisitid | itemname | itemtime
1 | 10 | 2 | box | 23
2 | 10 | 2 | clock | 70
3 | 10 | null | water | 50
4 | 10 | null | paper | 40
5 | 12 | 3 | box | 26
What I have now is this:
$query = "SELECT visitid, visitdate, visitreason FROM visits WHERE personid = '10' ORDER BY visitdate DESC";
// 2nd select: "SELECT itemid, itemname, itemtime FROM items WHERE itemvisitid= visitid";
$db->setQuery($query);
$results = $db->query();
while($row = mysqli_fetch_array($results)){
echo "<tr>
<td>".$row['visitid'].", ".$row['visitdate']."</td>
<td>".$row['visitreason']."</td>
<td>".$row['itemid'].",".$row['itemname'].", ".$row['itemtime']."</td>
</tr>";
}
I need results to be something like this:
<tr>
<td>1, 05/07/2014</td><td>no reason</td><td></td>
<td>2, 06/07/2014</td><td>some reason</td><td>1, box, 23<br />2, clock, 70</td>
<td>4, 12/07/2014</td><td>some other reason</td><td></td>
</tr>
I guess your might to use GROUP_CONCAT like this:
DEMO: http://sqlfiddle.com/#!2/9d4e22/15
SELECT visitid, DATE_FORMAT(visitdate,'%m/%d/%Y'), visitreason,
GROUP_CONCAT(itemid,itemname, itemtime)
FROM visits left join items on visits.visitid = items.itemvisitid
WHERE visits.personid = 10
GROUP BY visitid, visitdate, visitreason
You might want to read this to know GROUP_CONCAT :
How to use GROUP_CONCAT in a CONCAT in MySQL
The document of GROUP_CONCAT() is here:
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
hope this helps.
SELECT `visits`.`visitid`, `visits`.`visitdate`, `visits`.`visitreason`,`items`.`itemname`, `items`.`itemtime` from `visits` INNER JOIN `items` ON `visits`.`personid`=`items`.`personid` WHERE `visits`.`personid` = '10' ORDER BY `visits`.`visitdate` DESC
if there is any error please change the field name personid in 'items' table.and then check.
This query:
SELECT v.visitid,
v.visitdate,
v.visitreason,
i.itemid,
i.itemname,
i.itemtime
FROM visits v
INNER JOIN items i
ON ( v.visitid = i.itemvisitid )
WHERE v.person_id = 10
ORDER BY v.visitdate DESC,
i.itemid ASC
will link both tables and produce a resultset that you can traverse using a double loop. The outer loop to process changes to the visit, and the inner to add every item visited in a particular visit.

how to display mysql latest entries from each category

we re using CodeIgniter, we want to get 6 categories latest 10 entries by (published_date), then we will display different categories results in view. here we are using 6 colourful boxes to display 10 latest entries from each category. our SQL tables are look like this ...
-> ci_categories
cat_id | cat_name | cate_slug | cate_title
-> ci_pages
page_id | cat_id | page_title | published_date
--------------------------------------
1 | 1 | Ttl 1 | 2014-02-22 10:22:20
2 | 2 | Ttl 2 | 2014-02-24 11:42:30
3 | 1 | Ttl 3 | 2014-02-26 10:37:21
4 | 3 | Ttl 3 | 2014-02-28 12:40:30
SELECT `ci_pages`.`cat_id` AS CAT_ID,
(SELECT `cat_name` FROM `ci_categories` WHERE `ci_categories`.`cat_id` = `ci_pages`.`cat_id`) AS CAT_NAME,
(SELECT `cat_slug` FROM `ci_categories` WHERE `ci_categories`.`cat_id` = `ci_pages`.`cat_id`) AS CAT_SLUG,
(SELECT `cat_title` FROM `ci_categories` WHERE `ci_categories`.`cat_id` = `ci_pages`.`cat_id`) AS CAT_TITLE
FROM `ci_pages`
ORDER BY `ci_pages`.`published_date` DESC
LIMIT 10;
This will return the 10 last inserted values into "ci_pages" Table getting each Category's details from "ci_pages" table. Check now...
try this query..
select category.*,pages.* from ci_categories category join ci_pages pages on (category.cat_id = pages.cat_id) group by pages.cat_id order_by pages.published_date DESC limit 0,10

Categories