Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   SQL (https://www.clubdelphi.com/foros/forumdisplay.php?f=6)
-   -   clausula COUNT con Mysql (https://www.clubdelphi.com/foros/showthread.php?t=46733)

karlaoax 07-08-2007 19:12:37

clausula COUNT con Mysql
 
Hola a todos de nuevo, miren tengo un codigo que estoy tratando de hacer. Lo unico que voy a hacer es contar de una Tabla todos los campos que cumplan con la condicion que sean KELLOGS por medio de la clausula COUNT.
Tengo mi tabla PRODUCTOS con los campos (Clave,nombre,marca)
Y mi tabla MARKET, en donde al momento de guardar, solo guardo la clave, sus campos (clave,producto)
Ambas estan unidas por clave primaria CLAVE

Query Venta:

Código SQL [-]
SELECT count(cereal) AS cereal,
(SELECT count(barritas) FROM market WHERE barritas='Special' and marca='kellogs'),
(SELECT count(Wafles) FROM market WHERE wafles ='Special' and marca='kellogs'),
(SELECT count(pan) FROM market WHERE pan='Special' and marca='kellogs')
AS barritas FROM market
WHERE cereal = 'cereal' and marca = 'Kellogs'

BOTON:

Código Delphi [-]
QueryMarket.Open;
try        
  txtcereal.Text:=QueryMarket.Fields[0].AsString;
  txtbarritas.Text:=QueryMarket.Fields[1].AsString;
  txtwafles.Text:=QueryMarket.Fields[2].AsString;
  txtpan.Text:=QueryMarket.Fields[3].AsString;
finally
QueryVenta.Close;
end;

No se como unir la tabla PRODUCTOS y Market, ya intente con JOIN pero sigue sin mostrarme los resultados en los TXTedits que menciono: txtcereal,txtbarritas,txtwafles,txtpan.

Me podrian decir si mi query esta mal implementada, utilizaria una clave foranea con MArca?

Solo tiene que contar aquellos que cuentan con marca KELLOGS, de mi tabla Market. Unidos por clave de la tabala PRODUCTOS

Espero no haberlos confundido.
Karla

15:22 pm Monterrey
Parece que ya solucione el problema, seguire haciendo pruebas hasta estar 100% segura

AzidRain 08-08-2007 04:23:27

haz un query por cada linea de producto, es mas rápido:




Código Delphi [-]

sql_str := 'select count(if(barritas='Special' and marca='kellogs', 1,null) as c_barritas,'+
          'count(if(wafles='Special and marca='kellogs',1,null) as c_wafles,'+
          'count(if(pan='Special and marca='kellogs',1,null) as c_pan,'+ 
          'count(cereal) as c_cereal from market';
         
QueryMarket.SQL.Clear;
QUeryMarket.SQL.Add(sql_str);
QueryMarket.Open;

    
  txtcereal.Text:=QueryMarket.fieldByName('c_cereral').AsString;
  txtbarritas.Text:=QueryMarket.FieldBYName('c_barritas').AsString;
  txtwafles.Text:=QueryMarket.FieldByName('c_wafles').AsString;
  txtpan.Text:=QueryMarket.FieldByNAme('c_pan').AsString;
finally
QueryMarket.Close;  //Esto estaba mal cerrabas ventas y es market


La franja horaria es GMT +2. Ahora son las 03:44:28.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi