Oto dwie tabele:
tbl_carts:
CREATE TABLE `tbl_carts` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`session_id` varchar(32) NOT NULL DEFAULT '',
`user_id` int(10) NOT NULL DEFAULT '0',
`order_nr` int(10) NOT NULL DEFAULT '0',
`payment_id` tinyint(4) NOT NULL DEFAULT '0',
`color` varchar(6) NOT NULL DEFAULT '0',
`date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`cart_status` int(3) NOT NULL DEFAULT '0',
`user_comment` tinytext NOT NULL,
`user_ip` varchar(15) NOT NULL DEFAULT '000.000.000.000',
`tmp_address` int(10) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=111 ;
tbl_carts_products:
CREATE TABLE `tbl_carts_products` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`cart_id` int(6) NOT NULL DEFAULT '0',
`product_id` int(8) NOT NULL DEFAULT '0',
`item_md5` varchar(32) NOT NULL DEFAULT '0',
`date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`amount` tinyint(3) NOT NULL DEFAULT '0',
`price` decimal(9,2) NOT NULL DEFAULT '0.00',
`properties` tinytext NOT NULL,
`art_code` tinytext NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
jest to tabela koszyka i tabela produktów w koszyku, łączenie odbywa się po cart_id i jest to łączenie jeden do wielu, tbl_carts.id(jeden)<----(wiele)tbl_carts_products.cart_id
chciałbym stworzyć zapytanie które wybierze mi id koszyka i odpowiednio do tego sumę przedmiotów tegoż koszyka, warunek: (data ostatniej aktualizacji koszyka + zdefiniowany czas wygaśnięcia) mniejsze niż (czas teraźniejszy)
oto moje zapytanie:
<?php
$query = 'SELECT tc.id COUNT(tcp.cart_id) AS pr_amount
FROM tbl_carts AS tc, tbl_carts_products AS tcp
WHERE (tc.date + '.$this->exp_time.') < NOW()
GROUP BY tcp.cart_id;';
?>
i błąd który pokazuje:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'COUNT(tcp.cart_id) AS pr_amount FROM tbl_carts AS tc, tbl_carts_products AS tcp ' at line 1
wersja MySQL: 4.1.12a-nt
wiem że problem jest z GROUP BY, a może powinienem użyć JOIN, tak czy inaczej nie wiem jak to zrobić, proszę o pomoc.
pozdrawiam
Ten post edytował krowal 31.05.2007, 07:49:45