Jump to content

Help with mySql query


inversesoft123

Recommended Posts

I have a following fields in mySQL database.

 

1. ups = thumb up for content (votes)

2. downs = thumb down for the content (votes)

 

Now I want to create next and previous navigation links for required data and my query is.

 

$subfactor = $currentfile['ups'] - $currentfile['downs'];
$albumid =  $currentfile['id'];

  $selectprevlink="SELECT * from pf_table where (((ups - downs) <= $subfactor) AND (id != $albumid)) ORDER by (ups - downs) DESC LIMIT 1";
  $selectprevlink2=mysql_query($selectprevlink);
  $selectprevlink3=mysql_fetch_array($selectprevlink2);

 

(id < $albumid) above is also giving wrong results.

 

Please help me with this query here I want to get next file from database hence I have included id != (current album id).

However the code bellow works perfect if value of (ups - down) is not equal to $subfactor

  $selectprevlink="SELECT * from pf_table where (ups - downs) < $subfactor ORDER by (ups - downs) DESC LIMIT 1";
  $selectprevlink2=mysql_query($selectprevlink);
  $selectprevlink3=mysql_fetch_array($selectprevlink2);

 

Thanks, very much in advance.. :)

Link to comment
https://forums.phpfreaks.com/topic/205381-help-with-mysql-query/
Share on other sites

If posts has same number of ups and downs then it will rank automatically according to unique id and it works fine in index page. but when we browse content then along with back link I am trying to include quick next and previous links but getting different records from database.

 

Like in your example

 

(5 - 2) = 3

(5 - 3) = 2

(4 - 3) = 1

(5 - 5) = 0

 

5 - 5 goes last

So how about something like SELECT TOP 1 * FROM table WHERE (for - against) > $voting_score OR ((for-against) = $voting_score and id > $current_id) ORDER BY (for - against) ASC, id ASC

 

Sorry about it being so pseudocody instead of specific. You'll have to translate to your exact code. That code is meant to have all interpolations of variables into the SQL string escaped using something like mysql_real_escape_string() as well.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.