Jump to content

Combining two sql querys


smith.james0

Recommended Posts

I have had a good look on google and a few evening trying to get it to work, but I am no further. I have two querys

 

The first is to get the all the information for the page from db main

SELECT *  FROM main WHERE Category = '$sql_Category' ORDER BY  `main`.`Name` ASC LIMIT $page, $limit

and the second is to get the rating for the first 

SELECT `item`, (`totalrate` / `nrrates`) AS `rank` FROM `rtgitems` ORDER BY (`totalrate` / `nrrates`) DESC 

The item column in rtgitems uses the id from the main table as a reference between the two.

 

I would like to sort the result in the first query by ranking score taken from the second table like

 

SELECT * FROM main WHERE Category = '$sql_Category' ORDER `item`, (`totalrate` / `nrrates`) AS `rank` FROM `rtgitems` ORDER BY (`totalrate` / `nrrates`) DESC

 

can this be done?

 

James

 

 

Link to comment
Share on other sites


SELECT main.*, rtgitems.item, (rtgitems.totalrate / rtgitems.nrrates) AS rank

FROM main
JOIN rtgitems ON rtgitems.item = main.id

WHERE main.Category = '$sql_Category'

ORDER BY main.`Name` ASC, rtgitems.totalrate / rtgitems.nrrates) DESC

LIMIT $page, $limit
Edited by Psycho
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.