How to show top 10 with most views? - php

I want to show the top 10 seeded torrents on my website but according to hits(views) & date added.
Here's code that i'm using this code shows top 10 most seeded torrents. But i want to show top 10 most seeded torrents of last 24hrs. After 24hrs of torrent uploaded.It should be replaced by next most seeded torrent of last 24hrs.
$movie = "
SELECT t.id
, t.anon
, t.announce
, t.category
, t.leechers
, t.nfo
, t.seeders
, t.name
, t.times_completed
, t.size
, t.added
, t.comments
, t.numfiles
, t.filename
, t.owner
, t.external
, t.freeleech
, c.name AS cat_name
, c.image AS cat_pic
, c.parent_cat AS cat_parent
, u.username
, u.privacy
, IF(t.numratings < 2, NULL, ROUND(t.ratingsum / t.numratings,1)) rating
FROM torrents t
LEFT
JOIN categories c
ON c.id = t.category
LEFT
JOIN users u
ON u.id = t.owner
WHERE visible = 'yes'
AND banned = 'no'
AND c.parent_cat = 'Movie'
ORDER
BY t.seeders + t.leechers + t.hits DESC
, t.seeders DESC
, t.added DESC
LIMIT 10
";
Please solve this i'm trying to solve this from last Month. Or If possible Torrent with most views from last 24hrs should be shown on top.

Whats your torrent tables schema ?
is torrents.added DATETIME column or just regular INT for unix timestamps ?
$movie = "
SELECT t.id
, t.anon
, t.announce
, t.category
, t.leechers
, t.nfo
, t.seeders
, t.name
, t.times_completed
, t.size
, t.added
, t.comments
, t.numfiles
, t.filename
, t.owner
, t.external
, t.freeleech
, c.name AS cat_name
, c.image AS cat_pic
, c.parent_cat AS cat_parent
, u.username
, u.privacy
, IF(t.numratings < 2, NULL, ROUND(t.ratingsum / t.numratings,1)) rating
FROM torrents t
LEFT
JOIN categories c
ON c.id = t.category
LEFT
JOIN users u
ON u.id = t.owner
WHERE visible = 'yes'
AND banned = 'no'
AND c.parent_cat = 'Movie'
AND t.added > DATE_SUB(NOW(), INTERVAL 1 DAY)
ORDER
BY t.seeders + t.leechers + t.hits DESC
, t.seeders DESC
, t.added DESC
LIMIT 10
";

Just add to the WHERE clause:
WHERE visible = 'yes' AND banned = 'no'
AND categories.parent_cat= 'Movie'
AND torrents.uploaded_date > DATE_SUB(NOW(), INTERVAL 1 DAY)

Related

LIMIT results on specific table when using LEFT JOIN

Below is my query. I would like to get only 10 posts from post table. However it LIMIT 10 below doesn't limit the results in posts table but it does on another table.
Can anyone help me to fix the query? I really appreicate your help.
SELECT posts.id , posts.cat_id , posts.school_id , posts.campus_id , posts.status , posts.priority , posts.title , posts.content , posts.phone , posts.email , posts.tags , posts.zip , posts.price_new , posts.price_old , posts.reviewed_by , posts.reviewed_date , posts.updated_date , posts.posted_date , posts.expired_date , posts.ip_address , schools.school_name, campuses.campus_name , meta.meta_key , meta_value , images.img_name
FROM posts LEFT JOIN
meta
ON posts.id = meta.post_id LEFT JOIN
images
ON posts.id = images.post_id LEFT JOIN
schools
ON posts.school_id = schools.id LEFT JOIN
campuses
ON posts.campus_id = campuses.id
ORDER BY posts.updated_date DESC LIMIT 10
One method is to use a subquery:
SELECT p.id , p.cat_id, p.school_id, p.campus_id , p.status,
p.priority, p.title, p.content, p.phone, p.email, p.tags, p.zip,
p.price_new, p.price_old, p.reviewed_by, p.reviewed_date,
p.updated_date, p.posted_date, p.expired_date, p.ip_address,
s.school_name, c.campus_name,
m.meta_key, m.meta_value,
i.img_name
FROM (SELECT p.*
FROM posts p
ORDER BY p.updated_date DESC
LIMIT 10
) p LEFT JOIN
meta m
ON p.id = m.post_id LEFT JOIN
images i
ON p.id = i.post_id LEFT JOIN
schools s
ON p.school_id = s.id LEFT JOIN
campuses c
ON p.campus_id = c.id;
Note that the use of table aliases makes the query easier to write, read, and understand.

mysql query not fetching data from where condition

I have written a MySQL query ?I want to fetch data t.taskid = b.id OR t.taskid = '0' AND t.subtaskid = j.id OR t.subtaskid = '0' from this but where condition is not working for this
SELECT t.id as id
, t.taskid
, t.subtaskid
, p.PNAME AS projectname
, t.date AS DATE
, b.Summary AS tasks
, j.SUMMARY AS subtasks
, t.mon AS time1
, t.tue AS time2
, t.wed AS time3
, t.thu AS time4
, t.fri AS time5
, t.sat AS time6
, t.sun AS time7
FROM bt_createissue b
, bt_createissue j
, timesheet t
LEFT
JOIN bt_project p
ON t.projectid = p.pkey
WHERE t.taskid = b.id
OR t.taskid = 0
AND t.subtaskid = j.id
OR t.subtaskid = 0
AND t.date >= '2015-03-09'
AND t.date <= '2015-03-30'
AND t.username = '$username'
If t.taskid = b.id
t.subtaskid = j.id are to be joins then check whether you can modify
WHERE t.taskid = b.id OR t.taskid = '0'
AND t.subtaskid = j.id OR t.subtaskid = '0'
the above lines with proper joins.
Because using join conditions in where clause can create cross join instead of intended one.
If it do not work
Try with using having clause instead of where clause and don't forget to use group by

Mysql join with group by and order by

I am using this mysql query
SELECT `p`.`id` AS product_id, `p`.`title` , `i`.`image` , `u`.`user_username` , `m`.`id` AS message_id, `m`.`date` , `m`.`from` , `m`.`to` , `m`.`message` , `m`.`read`
FROM (`messages` AS m)
JOIN `products` AS p ON `m`.`product_id` = `p`.`id`
JOIN `users` AS u ON `m`.`from` = `u`.`user_id`
JOIN `product_images` AS i ON `p`.`id` = `i`.`product_id`
WHERE `i`.`type` = 'FRONT'
AND (m.to = '1171' OR m.from = '1171)
GROUP BY `m`.`therad_user` , `m`.`product_id`
ORDER BY `m`.`id` DESC
In message table it shows message with oldest message id, I want to return latest message. but it show oldest message. how I solve this.
Try ORDER BY first, then only GROUP BY
SELECT * FROM
(
SELECT `p`.`id` AS product_id, `p`.`title` , `i`.`image` , `u`.`user_username` , `m`.`id` AS message_id, `m`.`date` , `m`.`from` , `m`.`to` , `m`.`message` , `m`.`read`
FROM (`messages` AS m)
JOIN `products` AS p ON `m`.`product_id` = `p`.`id`
JOIN `users` AS u ON `m`.`from` = `u`.`user_id`
JOIN `product_images` AS i ON `p`.`id` = `i`.`product_id`
WHERE `i`.`type` = 'FRONT'
AND (m.to = '1171' OR m.from = '1171)
ORDER BY `m`.`id` DESC
) tmp
GROUP BY `m`.`therad_user` , `m`.`product_id`

Joining two table and view extreme values - not work query properly

i have two table i joining both tables and want display extreme values of select one columns this is php/mysql code.
$sql="Select A.date, A.rainfall, A.evep ,
A.max_temp , A.min_temp , A.sunshine_hrs ,
B.temp_air , B.dry_temp ,B.wet_temp,
B.rel_humid , B.soil_temp_5, B.soil_temp_20,
B.soil_temp_30 , B.soil_temp_60 ,
B.air_pressure
FROM reg_data3 A
INNER JOIN reg_data2 B
ON A.date = B.date
WHERE year(DATE_FORMAT(A.date, '%y-%m-%d'))='".$year."'
and month(DATE_FORMAT(A.date, '%y-%m-%d'))='".$month."'
ORDER BY B.date and $paramiter > $value ";
when echo query; first i select $year=2008 $month=2 and $parameter=soil_temp_60 and $value=20
Select A.date, A.rainfall, A.evep , A.max_temp ,
A.min_temp , A.sunshine_hrs , B.temp_air ,
B.dry_temp ,B.wet_temp, B.rel_humid ,
B.soil_temp_5 ,B.soil_temp_20 , B.soil_temp_30 ,
B.soil_temp_60 , B.air_pressure
FROM reg_data3 A
INNER JOIN reg_data2 B
ON A.date = B.date
WHERE year(DATE_FORMAT(A.date, '%y-%m-%d'))='2008'
and month(DATE_FORMAT(A.date, '%y-%m-%d'))='02'
ORDER BY B.date and A.soil_temp_60 > 24 ;
extreme values in table related to this query but not work well
I think that your problem is with variables in wrong place, try this:
$sql="Select A.date, A.rainfall, A.evep ,
A.max_temp , A.min_temp , A.sunshine_hrs ,
B.temp_air , B.dry_temp ,B.wet_temp,
B.rel_humid , B.soil_temp_5, B.soil_temp_20,
B.soil_temp_30 , B.soil_temp_60 ,
B.air_pressure
FROM reg_data3 A
INNER JOIN reg_data2 B
ON A.date = B.date
WHERE year(DATE_FORMAT(A.date, '%y-%m-%d'))='".$year."'
and month(DATE_FORMAT(A.date, '%y-%m-%d'))='".$month."'
and $paramiter > $value
ORDER BY B.date ";

Complicated mysql join for student listing

I have this problem with my query. The goal of the query is to display all students, whether they are in the aanweezigheid table or not.
This is my query:
SELECT
s.studentNaam
, s.studentAchterNaam
, s.studentStamNummer
, s.klasID
, k.klasNaam
, k.klasID
, a.studentID
, a.aanwezigTijdAan
, a.aanwezigTijdAf
, a.aanwezigDag
, a.aanwezigStatus
FROM studenten AS s
LEFT JOIN klassen AS k ON s.klasID=k.klasID
LEFT JOIN aanweezigheid AS a ON a.studentID=s.studentID
WHERE k.klasNaam = 'MD2a'
AND a.aanwezigDag='2012-08-28'
ORDER BY s.studentAchterNaam ASC
Any ideas?
Move your WHERE conditions to LEFT JOIN ON clause:
SELECT ...
FROM studenten AS s
LEFT JOIN klassen AS k
ON s.klasID=k.klasID
AND k.klasNaam = 'MD2a'
LEFT JOIN aanweezigheid AS a
ON a.studentID=s.studentID
AND a.aanwezigDag='2012-08-28'
ORDER BY s.studentAchterNaam ASC;
just move your "a" dependency into the ON term. That way you won't be filtering anything out in your WHERE:
SELECT
s.studentNaam
, s.studentAchterNaam
, s.studentStamNummer
, s.klasID
, k.klasNaam
, k.klasID
, a.studentID
, a.aanwezigTijdAan
, a.aanwezigTijdAf
, a.aanwezigDag
, a.aanwezigStatus
FROM studenten AS s
LEFT JOIN klassen AS k ON s.klasID=k.klasID
LEFT JOIN aanweezigheid AS a ON a.studentID=s.studentID
AND a.aanwezigDag='2012-08-28'
WHERE k.klasNaam = 'MD2a'
ORDER BY s.studentAchterNaam ASC

Categories