开发者

Using sub-query inside "IN" operator?

I'm extending a basic shopping cart system and need a way to show all invoices in which a given product was purchased. I'm not sure a SELECT can be done this way, but I could a boost.

The problem is that invoice IDs correspond to shopping cart entries which identify the product IDs being purchased. I am querying for all purchases within a given category of products (p.categoryid).

SELECT i.id, i.name, i.totalprice, i.dateof
FROM invoices i
WHERE i.status > '1' AND i.id IN (
    SELECT c.invoice FROM maj_cart c, maj_products p WHERE c.pid = p.pid AND p.categoryid = '43'
)

Here is my database structure:

CREATE TABLE IF NOT EXISTS `maj_cart` (
`cid` int(10) NOT NULL auto_increment,
`pid` int(10) NOT NULL default '0',
`sessid` varchar(50) NOT NULL default '',
`dateof` int(10) NOT NULL default '0',
`price` decimal(10,2) NOT NULL,
`shipping` decimal(10,2) NOT NULL,
`discount` decimal(10,2) NOT NULL,
`quantity` int(10) NOT NULL default '0',
`total` decimal(10,2) NOT NULL,
`invoice` int(10) NOT NULL default '0',
`status` int(1) NOT NULL,
PRIMARY KEY  (`cid`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 ;

CREATE TABLE IF NOT EXISTS `maj_invoices` (
`id` int(10) NOT NULL auto_increment,
`dateof` int(10) NOT NULL default '0',
`userid` int(10) NOT NULL default '0',
`sessid` varchar(50) NOT NULL default '',
`name` varchar(255) NOT NULL default '',
`company` varchar(150) NOT NULL,
`email` varchar(255) NOT NULL default '',
`address1` varchar(255) NOT NULL default '',
`address2` varchar(255) NOT NULL default '',
`city` varc开发者_Go百科har(255) NOT NULL default '',
`state` char(3) NOT NULL default '',
`zip` varchar(10) NOT NULL default '',
`phone` varchar(40) NOT NULL default '',
`phone2` varchar(40) NOT NULL,
`instructions` text NOT NULL,
`recipmssg` text NOT NULL,
`promo` int(10) NOT NULL,
`discount` decimal(10,2) NOT NULL,
`totalprice` decimal(10,2) NOT NULL default '0.00',
`filename` varchar(20) NOT NULL,
`status` int(1) NOT NULL default '0',
`shipped` int(1) NOT NULL default '0',
`errorno` int(1) NOT NULL default '0',
`notes` text,
`source` int(5) NOT NULL default '0',
PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 PACK_KEYS=1 ;

CREATE TABLE IF NOT EXISTS `maj_products` (
`pid` int(10) NOT NULL auto_increment,
`itemnum` varchar(30) NOT NULL default '',
`filename` varchar(100) default NULL,
`original` varchar(255) default NULL,
`itemname` varchar(255) NOT NULL default '',
`descrip` text,
`summary` varchar(100) NOT NULL default '',
`categoryid` int(11) NOT NULL default '0',
`userid` int(10) NOT NULL default '0',
`dateadded` int(12) NOT NULL default '0',
`displayorder` int(10) NOT NULL default '0',
`price` decimal(10,2) NOT NULL default '0.00',
`shipping` decimal(10,2) NOT NULL default '0.00',
`instock` int(10) NOT NULL default '0',
`discount` int(10) NOT NULL,
`meta_keywords` text NOT NULL,
`meta_descrip` text NOT NULL,
PRIMARY KEY  (`pid`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 PACK_KEYS=1 ;


The suggested method would be just a normal join:

SELECT i.id, i.name, i.totalprice, i.dateof
FROM invoices i
INNER JOIN maj_cart c ON c.invoice = i.id
INNER JOIN maj_products p ON c.pid = p.pid
WHERE p.category_id = 43
AND i.status > '1'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜