Jump to content

obsessed with removing query from loop need help


Ujj

Recommended Posts

Hi again, :D

 

being obsessed with removing queries from loop to optimize my code as suggested, here is my a situation i am stuck while trying to avoid query inside loop

 

this is my t_item table

table1.png

 

and this is item_detail

table2.png

 

now i am running query like this to find out the lowest priced item from each track_item row of table t_item of each user_item of table t_item.

 

after that i also need to find out price for each user_item of t_item table..

 

 


$sqlAdjustPrice = "select * from t_item where `user_id` = 63";
$rst=mysql_query($sqlAdjustPrice);
while($row1=mysql_fetch_array($rst))

{
	$myItem = $row1['user_item'];	
	$trackedItem = $row1['track_item'];
	$adjustPrice = $row1['adjust_price'];
	$groupID = $row1['g_id'];


if($adjustPrice=='y')	{

$sql = "SELECT  price FROM item_detail WHERE itemID in ($trackedItem)  AND groupID='6' ORDER BY price asc LIMIT 1";	
$rs=mysql_query($sql);
$row = mysql_fetch_array($rs);
$lowestPricedTrackedItem = round($row['price'],2);	

	 $sql = "SELECT price FROM item_detail  WHERE `itemID` = '".$myItem."' and `user_id`='63' and groupID='6'";
      $rs=mysql_query($sql);
	$row = mysql_fetch_array($rs);
	$myItemPrice = $row['price'];


}

 

how can i avoid loop here please ?

Link to comment
Share on other sites

Please read up on the MySQL JOIN syntax, as it is the proper way of dealing with these kind of situations.

 

i tried but no success as I need to run this query for every user_item with condition where track_item IN.

Link to comment
Share on other sites

Just in case it takes 3 people to say it:  You are doing this wrong.  You cannot store comma-delimited lists in a single column.  Why not?  Because of this thread.  It causes this problem.  This problem cannot be solved.  Your database is not designed right.

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.