Help with query optimization query SUM

2006-02-06 Thread Reynier Perez Mira
Hi:
I'm develop a simple shopping cart. I have this two tables:
carro_de_compras
--
IDU int(11) NOT NULL
IDA int(11) NOT NULL
CantidadDeArticulos int(11) NOT NULL

os_articulo
--
IDA int(11) NOT NULL auto_increment,
IDC int(11) NOT NULL default '0',
ANombre varchar(200) NOT NULL default '', ADescripcion text, ACantidad int(11) 
NOT NULL default '0', AImagen varchar(50) default NULL, IDU int(11) NOT NULL 
default '0', APrecio float(6,2) default NULL, KEY AI_IDA (`IDA`)

Before ask let me explain some things. As you can see in the tables I have the 
same field IDU in both tables. So in first(table carro_de_compras) it means is 
user ID loged on ecommerce system, the second is the user ID who upload 
articles for sale. Something like eBay in wich you can sale and buy at every 
time. The arrive the point in wich I need to optimize queries:

PHP Code:
-
$sql = mysql_query(SELECT * FROM carro_de_compras); 
$sresultado = mysql_fetch_assoc($sql);

$query = mysql_query(SELECT * FROM os_articulo WHERE 
(IDA='.$sresultado['IDA'].')); 
while ($record = mysql_fetch_assoc($query)) {  
 $productos[] = $record; 
}

The question for this problem is: exists any way to optimize this query and 
leave only in one line? I read in MySQL doc about it and found some about JOIN 
but I can't understand how it works. Maybe because I'm cuban and not understand 
english as well as I want.

The other questions is how to add some values to a field. For example:
$sql = mysql_query(UPDATE table SET value=value+1 WHERE id='1');

For do this query I do this:
$sql = mysql_query(SELECT value FROM table WHERE id='1');
$result = mysql_query($sql);
$update = mysql_query(UPDATE table SET (value='.$result['value'].' + 1) 
WHERE id='1');

So is possible to optimize this query?


Regards
ReynierPM
4to. año Ing. Informática
Usuario registrado de Linux: #310201
*
El programador superhéroe aprende de compartir sus conocimientos. 
Es el referente de sus compañeros. Todo el mundo va a preguntarle y él, 
secretamente, lo fomenta porque es así como adquiere su legendaria
sabiduría: escuchando ayudando a los demás... 

--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]



Re: Help with query optimization query SUM

2006-02-06 Thread سيد هادی راستگوی حقی
Dear Reynier,

You can use JOIN on your both,
The JOIN have to run on the same feilds i.e IDA.

SELECT * FROM carro_de_compras LEFT JOIN os_articulo ON carro_de_compras.IDA
= os_articulo.IDA

This query returns all your users with their articles if any and you can
iterate on it.

but one note:
Use INDEX on both tables. You may encounter problems when your rows grow up.

about the UPDATE query:

UPDATE table SET value=value+1 WHERE id='1'

is enough, use that.


On 2/7/06, Reynier Perez Mira [EMAIL PROTECTED] wrote:

 Hi:
 I'm develop a simple shopping cart. I have this two tables:
 carro_de_compras
 --
 IDU int(11) NOT NULL
 IDA int(11) NOT NULL
 CantidadDeArticulos int(11) NOT NULL

 os_articulo
 --
 IDA int(11) NOT NULL auto_increment,
 IDC int(11) NOT NULL default '0',
 ANombre varchar(200) NOT NULL default '',

ADescripcion text,

ACantidad int(11) NOT NULL default '0',

AImagen varchar(50) default NULL,

IDU int(11) NOT NULL default '0',

APrecio float(6,2) default NULL,

KEY AI_IDA (`IDA`)

 Before ask let me explain some things. As you can see in the tables I have
 the same field IDU in both tables. So in first(table carro_de_compras) it
 means is user ID loged on ecommerce system, the second is the user ID who
 upload articles for sale. Something like eBay in wich you can sale and buy
 at every time. The arrive the point in wich I need to optimize queries:

 PHP Code:
 -
 $sql = mysql_query(SELECT * FROM carro_de_compras);
 $sresultado = mysql_fetch_assoc($sql);

 $query = mysql_query(SELECT * FROM os_articulo WHERE
 (IDA='.$sresultado['IDA'].'));
 while ($record = mysql_fetch_assoc($query)) {
 $productos[] = $record;
 }

 The question for this problem is: exists any way to optimize this query
 and leave only in one line? I read in MySQL doc about it and found some
 about JOIN but I can't understand how it works. Maybe because I'm cuban and
 not understand english as well as I want.

 The other questions is how to add some values to a field. For example:
 $sql = mysql_query(UPDATE table SET value=value+1 WHERE id='1');

 For do this query I do this:
 $sql = mysql_query(SELECT value FROM table WHERE id='1');
 $result = mysql_query($sql);
 $update = mysql_query(UPDATE table SET (value='.$result['value'].' + 1)
 WHERE id='1');

 So is possible to optimize this query?


 Regards
 ReynierPM
 4to. año Ing. Informática
 Usuario registrado de Linux: #310201
 *
 El programador superhéroe aprende de compartir sus conocimientos.
 Es el referente de sus compañeros. Todo el mundo va a preguntarle y él,
 secretamente, lo fomenta porque es así como adquiere su legendaria
 sabiduría: escuchando ayudando a los demás...

 --
 MySQL General Mailing List
 For list archives: http://lists.mysql.com/mysql
 To unsubscribe:
 http://lists.mysql.com/[EMAIL PROTECTED]




--
Sincerely,
Hadi Rastgou
a href=http://www.spreadfirefox.com/?q=affiliatesamp;id=0amp;t=1;Get
Firefox!/a