Jump to content

[SOLVED] PHP Combine Queries


amwd07

Recommended Posts

I really hope someone can help me here please, all I need to do is combine the 2 queries

joining the ratings query into $rs1 which must include the buildsql!

 

mysql_select_db($database_connDW, $connDW);
$query_rs1 = "SELECT * FROM restaurants WHERE ".$buildsql." AND onhold = '0' OR (accountname LIKE '$outlet' AND onhold = '0') OR (twn LIKE '$town' AND onhold = '0') ORDER BY old_image DESC";
$rs1 = mysql_query($query_rs1, $connDW) or die(mysql_error());
$row_rs1 = mysql_fetch_assoc($rs1);
$totalRows_rs1 = mysql_num_rows($rs1);

mysql_select_db($database_connDW, $connDW);
$query_overall_rate = "SELECT dine_id, AVG(total_value) FROM ratings GROUP BY dine_id";
$overall_rate = mysql_query($query_overall_rate, $connDW) or die(mysql_error());
$row_overall_rate = mysql_fetch_assoc($overall_rate);
$totalRows_overall_rate = mysql_num_rows($overall_rate);

Link to comment
Share on other sites

Firstly define for me what your relationship between the two tables is,  and on what key.

e.g.

restaurants ==== has many ===> ratings

or

restaurants ==== has 1 ===> rating

 

and

restaurant has rating_id

or rating has restaurant_id

or (some intermediary table)

Link to comment
Share on other sites

OK currently the ratings query is working to find the average, if I can combine the 2 queries I can use it in the repeat region for example 20 outlets listed on the page would work out the average votes for each outlet

 

the relationship for the 2 tables is dine_id

Link to comment
Share on other sites

Ok so assuming what you're telling me is correct, in that the dine_id is the ID index/key of the restaurant in question, your SQL shoud look something like this :

SELECT rest.*
          ,AVG(rat.total_value) as total_value
FROM restaurants rest
JOIN ratings rat ON rat.dine_id = rest.id
WHERE ".$buildsql." 
AND rest.onhold = '0' 
OR (accountname LIKE '$outlet' AND rest.onhold = '0') 
OR (rest.twn LIKE '$town' AND rest.onhold = '0') 
GROUP BY rest.id
ORDER BY rest.old_image DESC

 

Please check that the column is question is rest.id (line 4 & 9). I'm assuming you've called the primary key of your restaurant "id" but this might not be the case.

Link to comment
Share on other sites

I had to change the code slightly the average is correct but only the outlets rated are showing if i empty the ratings table no results are shown ???

 

mysql_select_db($database_connDW, $connDW);
$query_rs1 = "SELECT restaurants.*,AVG(ratings.total_value) as total_value
FROM restaurants JOIN ratings ON ratings.dine_id = restaurants.dine_id
WHERE ".$buildsql." AND restaurants.onhold = '0' OR (accountname LIKE '$outlet' AND restaurants.onhold = '0') OR (restaurants.twn LIKE '$town' AND restaurants.onhold = '0') GROUP BY ratings.dine_id ORDER BY restaurants.old_image DESC";
$rs1 = mysql_query($query_rs1, $connDW) or die(mysql_error());
$row_rs1 = mysql_fetch_assoc($rs1);
$totalRows_rs1 = mysql_num_rows($rs1);

Link to comment
Share on other sites

I think there's something funny about your structure + data, nevertheless here is a modified version of what I was doing :

 

SELECT rest.*, x.total_value
FROM restaurants rest
JOIN 
  (SELECT IFNULL(AVG(total_value),0) as total_value
     FROM ratings
     GROUP BY dine_id
  ) x ON x.dine_id = rest.dine_id
WHERE ".$buildsql." 
AND rest.onhold = '0' 
OR (accountname LIKE '$outlet' AND rest.onhold = '0') 
OR (rest.twn LIKE '$town' AND rest.onhold = '0') 
ORDER BY rest.old_image DESC

Link to comment
Share on other sites

this works for me i have also added the total votes

couldn't of done it without you thanks  :D

 

SELECT restaurants.*,AVG(ratings.total_value) as avg_total_value, AVG(ratings.total_votes) as avg_total_votes FROM restaurants LEFT JOIN ratings ON ratings.dine_id = restaurants.dine_id

WHERE ".$buildsql." AND restaurants.onhold = '0' OR (accountname LIKE '$outlet' AND restaurants.onhold = '0') OR (restaurants.twn LIKE '$town' AND restaurants.onhold = '0') GROUP BY restaurants.dine_id ORDER BY restaurants.old_image DESC

 

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.